开发者

Rendering content inside a partial via =yield

开发者 https://www.devze.com 2023-03-30 06:53 出处:网络
I\'m creating an application with ruby on rails where I have an items/_item.html.erb. Inside the partial is a yield statement so i can add extra content as needed. In this case, I want to add a specif

I'm creating an application with ruby on rails where I have an items/_item.html.erb. Inside the partial is a yield statement so i can add extra content as needed. In this case, I want to add a specific button to item depending on what view calls partial.

This is what I've tried and it renders the partial, but it does not render the 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 have also tried using content_for and some other stuff with no success. Is there a way to be able to render specific content inside a partial via yield? I'm currently using Rails3

EDIT:

I've found out that it's the :collection hash that makes it impossible insert the block.

Both of this pieces of code work:

<%= render :layout => 'items/it开发者_高级运维em' do %>
      Hello world      
<% end %>

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

This means that if i do a .each i could accomplish what I want but it would be ugly code. Anyone know a way around this?


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

somewhere.html.erb

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

_item.html.erb

<% if content_for? :foobar %>
  <%= yield :foobar %>
<% end %>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号