Passing variables through multiple partials (rails 4)

大憨熊 提交于 2019-12-13 05:47:56

问题


I am trying to pass instance variables through multiple partials and having trouble. 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 ) %>

UPDATE:

I'm starting to think it might be that the instance variable @product is just not being set/passed for the redirect. Could the following be causing the issue? Opened different issue here:

Instance variable not set with redirect


回答1:


You have to use this syntax:

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 ) %>

Notice the use of partial: and locals:




回答2:


If you want to pass local variables, you should use this syntax:

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

For reference: http://guides.rubyonrails.org/layouts_and_rendering.html (3.4.4)



来源:https://stackoverflow.com/questions/35104715/passing-variables-through-multiple-partials-rails-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!