Rails 部分中的 :object 有哪些方法可用?
我正在尝试构建我的部分并减少重复,但我遇到了问题。以下工作正常:
在视图中:
<%= render 'valve' %>
部分:
<% if @valve.length > 1 %>
<h3>Valve kit</h3>
<%= render 'not_enough' %>
<% else if @valve.length < 1 %>
<h3>Valve kit</h3>
我将有很多套件,因此我不想对每个套件变量重复此操作,而是想做这样的事情:
<%= render :partial => 'valve', :object => @valve %>
<% if valve.length > 1 %>
<h3>Valve kit</h3>
<%= render 'not_enough' %>
<% else if valve.length < 1 %>
<h3>Valve kit</h3>
但这会破坏 Valve.length 方法。那么,当我将 @valve 作为 :object 传递时发生了什么变化?我可以使用另一种方法作为替代来完成部分中的相同功能吗?
I am trying to build my partials and repeat less, but I am having a problem. The following works just fine:
In view:
<%= render 'valve' %>
In partial:
<% if @valve.length > 1 %>
<h3>Valve kit</h3>
<%= render 'not_enough' %>
<% else if @valve.length < 1 %>
<h3>Valve kit</h3>
I am going to have many kits, so rather than repeating this for every kit variable, I would like to do something like this:
<%= render :partial => 'valve', :object => @valve %>
<% if valve.length > 1 %>
<h3>Valve kit</h3>
<%= render 'not_enough' %>
<% else if valve.length < 1 %>
<h3>Valve kit</h3>
But that breaks the valve.length method. So, what is changing when I pass @valve as the :object? Is there another method I can use as a substitute to accomplish the same functionality in the partial?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将 :locals 哈希传递给部分
然后在您的视图中使用 Valve.* 就像您所拥有的那样。尽可能避免在局部变量中使用实例变量。
You need to pass a :locals hash to the partial
Then use valve.* in your view like you have. Try to avoid using instance variables in partials when possible.