Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails partial's layouts with named yield - why is yield block never used?

I have a partial, with a layout:

<%= render :partial => 'home/mobile/home', :layout => 'home/mobile/page', :locals => {:page => 'abc2'}%>

The layout (page.html.erb) has yields for different blocks, such as:

<div data-role="header">
  <%= yield :header %>
</div>

However, this yield block is never used, while the main-level layout file does yield as one would expect.

Is it impossible to use named content_for/yield blocks with the layouts of partials? Are there workarounds?

I would expect inheritance-- content_for :header should first look for a yield :header in the partial's layout, and failing that, the main layout file. But this is not the case. The partial layout's yield :header is simply ignored.

like image 930
Peter Ehrlich Avatar asked Sep 25 '11 18:09

Peter Ehrlich


1 Answers

In a situation similar to yours, I replaced the yield with a call to content_for without a block. So in your example it would be simply:

<div data-role="header">
  <%= content_for :header %>
</div>

That worked for me. That yields in partials don't trickle up as you suggest may be a feature or a bug - but that's still appear to be how it works in Rails 4.1.8, 3 years down the line :)

like image 81
sameers Avatar answered Sep 19 '22 11:09

sameers