Forum: Ruby on Rails content_for only works inside a yield?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
phil (Guest)
on 2008-11-23 18:21
(Received via mailing list)
I have a partial that has a couple of content_for blocks in it. This
works fine if I render it from a view that will in itself get rendered
view yield :layout. But, if I try and render it directly from the
application layout none of the content_for stuff gets interpreted.

Two questions
1) Is this right?
2) If so, how can I always include a partial inside of my views then?

Christopher (Guest)
on 2008-11-24 09:47
(Received via mailing list)
I have a similar problem. Upgrading to Rails 2.2.2 from 2.1.2 i've
lost all view components that are inserted with content_for.

The content_for call sits inside a view helper, which in turn is used
inside the view (eg
If i move the content_for call to inside the view it works fine.

Any ideas out there?
phil (Guest)
on 2008-11-24 20:41
(Received via mailing list)
I've discovered something else.
If you have an rjs template that loads in a partial:

page.replace_html "current_channel", :partial => "edit_form"

and on that page you have:
<script type="text/javascript" charset="utf-8">
  function popup() {
    alert("way hay");

That javascript code is not being rendered. At all (as far as I can
aaron (Guest)
on 2008-12-04 10:58
(Received via mailing list)
Try: content_for(:cow) { 'moo' }
This: content_for(:cow, 'moo)
Szymon K. (Guest)
on 2008-12-05 18:56
(Received via mailing list)
Have the same problem.
Is it a bug in Rails 2.2? Did they change something with content_for
or capture/concat methods?

Jo D. (Guest)
on 2009-01-26 18:21
I've the same problem with content_for and yield. The contect of the
content_for is not inserted in the yeld tag of the layout.

I've upgraded from rails 2.1.2 to 2.2.2.

 In the view
<% content_for :footer do %>
   footer text here
<% end %>

In the Layout

<%= yield :footer %>

Anyone know what the problem is?
Stephen (Guest)
on 2009-02-06 18:00
(Received via mailing list)
Has Anyone found a solution to this?   My setup is like this

layout (pseudo-code)

  <%= yield : head %>
     <%= render :partial => 'home/menu' %>
    <%= yield %>

In my partial I have:

<% content_for :head do %>
<% end %>
Some Html blah blah..

And it is not rendering the content into my header thats defined in the
partial thats rendered in the layout.  I'm using some of the YUI stuff
I'm bundling the javascript and the html in the same file so it stays
together nice and neat.

Stephen Blackstone
Matt J. (Guest)
on 2009-02-07 22:39
(Received via mailing list)
The problem you've run into is that yield blocks have to be filled
before they are called - normally,
with the yields in the layout, everything else from the request will
render first. But since you're calling
content_for in the layout, things aren't working.

The quickest way to do what you're trying would be to put the html
part in a content_for block, and then
call the partial from the header (with a yield :menu_body where the
current partial is). That puts the blocks
into the correct order.

--Matt J.
This topic is locked and can not be replied to.