content_for only works inside a yield?

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?

Thanks!

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 #30 Pretty Page Title - RailsCasts). If i move the content_for call to inside the view it works fine.

Any ideas out there?

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: _edit_form.rhtml: <script type="text/javascript" charset="utf-8">   function popup() {     alert("way hay");   } </script>

That javascript code is not being rendered. At all (as far as I can tell)

Try: content_for(:cow) { 'moo' } This: content_for(:cow, 'moo)

Have the same problem. Is it a bug in Rails 2.2? Did they change something with content_for or capture/concat methods?

-Szymon

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?

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 and I’m bundling the javascript and the html in the same file so it stays together nice and neat.

Regards, Stephen Blackstone

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 Jones