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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A07f0662e96a632d9447a710adadbe05?d=identicon&s=25 phil (Guest)
on 2008-11-23 17: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?

Thanks!
57e3855c26bec5183a6d8369d7122709?d=identicon&s=25 Christopher (Guest)
on 2008-11-24 08: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
http://railscasts.com/episodes/30-pretty-page-title).
If i move the content_for call to inside the view it works fine.

Any ideas out there?
A07f0662e96a632d9447a710adadbe05?d=identicon&s=25 phil (Guest)
on 2008-11-24 19: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:
_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)
D97cbdb181a1f6dde64461cbeb5bdbae?d=identicon&s=25 aaron (Guest)
on 2008-12-04 09:58
(Received via mailing list)
Try: content_for(:cow) { 'moo' }
This: content_for(:cow, 'moo)
A163c7a040af98ab8e1c823be26d723c?d=identicon&s=25 Szymon Kurcab (simonx1)
on 2008-12-05 17: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?

-Szymon
4f26469a063b6982695332df3d1ede5d?d=identicon&s=25 Jo Da (pwoods)
on 2009-01-26 17: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?
497c258e20915c2b16947525a9bb9833?d=identicon&s=25 Stephen (Guest)
on 2009-02-06 17:00
(Received via mailing list)
Has Anyone found a solution to this?   My setup is like this

layout (pseudo-code)

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

In my partial I have:

<% content_for :head do %>
  <script>someJavaSCript</script>
<% 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
6883e5ef03484d4fcef507d7b4f1d243?d=identicon&s=25 Matt Jones (Guest)
on 2009-02-07 21: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 Jones
This topic is locked and can not be replied to.