Rails 4 Passing multiple variables through partial mapping

This question has been asked many times, but I cannot get it to work.

I want to pass multiple vars to my partial ones, like this ...

<%= f.fields_for :materials do |builder| %> <%= render partial: 'material_fields', locals: {f: builder, feed: true} %> <% end %> 

And here is the line from partial material_fields.html.erb in which I want f.select to be pre-populated with the Yes or "true" parameter. (in some cases I want it to be false)

 <%=f.select :is_feed, options_for_select([['Yes', true], ['No', false]], feed )%> 

f is available and works until feed is ..... I don't know why this is not working. Ive tried <% = feed%> outside of the select statement, and it still doesn't work.

I get an undefined local variable or `feed 'method in both cases.

Does anyone know what is wrong with my syntax?

+5
source share
2 answers

I realized 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 the same file, the parameters are mixed up. Further testing may isolate the problem, but I have no energy or time.

Decision. Declare the same options for both renders. The values ​​may 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 %> 
+5
source

It is not clear what you are trying to do.

If this is on the browse page, which I assume is due to the erb tag, then you usually imagine a partial in which local variables should be passed in the locals hash:

 <%= render partial: 'material_fields', locals: {:f => builder, :feed => true} %> 

http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials

+1
source

Source: https://habr.com/ru/post/1207226/


All Articles