This question has been asked many times but I can not get it to work.
I want to pass multiple vars to my partial like this...
<%= f.fields_for :materials do |builder| %>
<%= render partial: 'material_fields', locals: {f: builder, feed: true} %>
<% end %>
And here is a line from partial material_fields.html.erb which I want the f.select to be pre-populated with the Yes option or "true" value. (some instances I want it to be false)
<%=f.select :is_feed, options_for_select([['Yes', true], ['No', false]], feed )%>
f
is available and works while feed
is not .....I dont know why this doesn't work.
Ive tried <%= feed %> outside the select statement and it still does not work.
I get undefined local variable or method `feed' in both cases.
Anyone know what is wrong with my syntax?
I figured out what the problem is.
I have
<%= f.fields_for :materials do |builder| %>
<%= render partial: 'material_fields', locals: {f: builder, feed: true } %>
<% end %>
and later in the same view I have
<%= f.fields_for :materials do |builder| %>
<%= render 'material_fields', f: builder %>
<% end %>
Apparently when rendering the same partial twice from one file the params are messed up. Further testing could isolate the issue but I have not the energy or time.
Solution: Declare the same params on both renders. The values can be different and they work as expected.
<%= f.fields_for :materials do |builder| %>
<%= render partial: 'material_fields', locals: {f: builder, feed: true } %>
<% end %>
<%= f.fields_for :materials do |builder| %>
<%= render partial: 'material_fields', locals: {f: builder, feed: false } %>
<% end %>
It's not clear what you're trying to do.
If this is in a view page, which i assume it is because of the erb tag, then you would normally be rendering a partial, in which local variables need to be passed through in the locals
hash:
<%= render partial: 'material_fields', locals: {:f => builder, :feed => true} %>
http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With