将变量传递给多个部分(轨道4)

时间:2016-01-30 17:22:58

标签: ruby-on-rails ruby-on-rails-4 partials

我试图通过多个部分传递实例变量并遇到麻烦。 undefined local variable or method "product"

Application.html.erb"

    <%= render 'shared/footer', :product => @product %>

_footer.html.erb

<%= render 'shared/my_form', :product => product %>

_my_form.html.erb

<%= form_for( product ) %>

更新:

我开始认为可能是实例变量@product没有为重定向设置/传递。以下可能导致问题吗?在这里打开了不同的问题:

Instance variable not set with redirect

2 个答案:

答案 0 :(得分:1)

如果要传递局部变量,则应使用以下语法:

 <%= render partial: 'shared/footer', locals: { product: @product } %>
 <%= render partial: 'shared/my_form', locals: { product: product } %>
 <%= form_for( product ) %>

供参考:http://guides.rubyonrails.org/layouts_and_rendering.html(3.4.4)

答案 1 :(得分:1)

您必须使用以下语法:

<强> Application.html.erb“

<%= render partial: 'shared/footer', locals: {:product => @product} %>

<强> _footer.html.erb

<%= render partial: 'shared/my_form', locals: {:product => product} %>

<强> _my_form.html.erb

<%= form_for( product ) %>

请注意使用partial:locals: