Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4 Passing multiple variables through render partial

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?

like image 663
JerryA Avatar asked Nov 19 '14 15:11

JerryA


2 Answers

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 %>
like image 186
JerryA Avatar answered Nov 14 '22 22:11

JerryA


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

like image 36
Max Williams Avatar answered Nov 14 '22 22:11

Max Williams