Making content inside partial via = yield

I am building an application from ruby ​​to rails where I have elements /_item.html.erb. There is a yield statement inside partial, so I can add additional content as needed. In this case, I want to add a specific button to the element, depending on which view causes a partial one.

This is what I tried and it displays partial, but it does not display a block:

_item.html.erb

<%= yield if block_given? %> <div> <%= item.name %> </div> 

someview.html.erb

 ... <% render(:partial => 'items/item', :collection => current_user.items do %> <%= "HELLO" %> <% end %> ... 

I also tried using content_for and some other things without success. Is there a way to make specific content inside partial with output? I am currently using Rails3

EDIT:

I found out what it is: a collection hash that makes block insertion impossible.

Both of these code snippets work:

 <%= render :layout => 'items/item' do %> Hello world <% end %> <%= render :layout => 'items/item', :locals => {:item => current_user.items.first} do %> Hello world <% end %> 

This means that if I do .each, I could do what I want, but that would be ugly code. Does anyone know about this?

+6
source share
1 answer

content_for should work fine in this case. Here is the code that I just double-checked locally.

somewhere.html.erb

 <% content_for :foobar do %> fubar <% end %> 

_item.html.erb

 <% if content_for? :foobar %> <%= yield :foobar %> <% end %> 
+4
source

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


All Articles