7

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?

JerryA
  • 310
  • 2
  • 15

2 Answers2

8

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 %>
JerryA
  • 310
  • 2
  • 15
  • Seems to be related -> http://stackoverflow.com/questions/4248970/rails-fields-for-render-partial-with-multiple-locals-producing-undefined-variabl?rq=1 – JerryA Nov 19 '14 at 21:13
2

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

Max Williams
  • 32,435
  • 31
  • 130
  • 197
  • I assumed you'd defined `f` already since it looked like you were trying to pass it through. You've changed your question quite a bit since i did this answer which confuses things a bit. – Max Williams Nov 19 '14 at 16:53
  • Sorry Max I really changed up the question to make it more clear what the problem is. – JerryA Nov 19 '14 at 19:57
  • I figured it out, and its a bug having to do with rendering the same partial twice from one file. Not something that you could have figured out from what I posted, sorry – JerryA Nov 19 '14 at 20:46