12

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.

Michael Durrant
  • 93,410
  • 97
  • 333
  • 497
Peter Ehrlich
  • 6,969
  • 4
  • 49
  • 65

2 Answers2

6

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 :)

sameers
  • 4,855
  • 3
  • 35
  • 44
0

The workaround would be to wrap your layout into an helper method using blocks (which should be able to yield correctly).

You may want to fil a bug about the original problem.

Oscar Del Ben
  • 4,485
  • 1
  • 27
  • 41