This project is archived and is in readonly mode.
Rails 3.0 yield :name doesn't show content_for :name
Reported by Matthew Daubert | September 26th, 2010 @ 03:00 PM
With Rails 3.0 yield(:name) doesn't yield the content defined in content_for(:name) when yield(:name) and content_for(:name) are called from different partials, both partials rendered from the same layout.
FAILING EXAMPLE (no content renders in the script tag)
<!-- app/views/layouts/application.html.erb -->
<html>
<%= render "shared/head" %>
<body>
<%= render "shared/messages" %>
<%= yield %>
</body>
</html>
<!-- app/views/shared/_head.html.erb -->
<head>
<script type="text/javascript">
<%= yield :script %>
</script>
</head>
<!-- app/views/shared/_messages.html.erb -->
<% if notice %>
<div id="notice_indicator"></div>
<% content_for :script do %>
alert("<%= escape_javascript(notice) %>");
<% end %>
<% end %>
PASSING EXAMPLE (content renders in script tag)
<!-- app/views/layouts/application.html.erb -->
<html>
<head>
<script type="text/javascript">
<%= yield :script %>
</script>
</head>
<body>
<%= render "shared/messages" %>
<%= yield %>
</body>
</html>
<!-- app/views/shared/_messages.html.erb -->
<% if notice %>
<div id="notice_indicator"></div>
<% content_for :script do %>
alert("<%= escape_javascript(notice) %>");
<% end %>
<% end %>
Comments and changes to this ticket
-
Jeff Kreeftmeijer September 27th, 2010 @ 07:03 AM
I was unable to reproduce it on master with this setup:
application.html.erb:
<%= render('some_partial') %>
users/_some_partial.html.erb:
<%= yield(:script) %>
users/index.html.erb:
<% content_for(:script) { 'works!' } %>
Can anyone else? I'll try to reproduce your exact example later, but I don't think it will make a difference. :)
-
Matthew Daubert September 27th, 2010 @ 12:47 PM
Thank you Jeff - Your example works for me, too. I think the problem may be specifically with calling content_for from within a partial and yielding in a different partial. I couldn't find anything in RDoc => CaptureHelper that would indicate that this shouldn't work and I thought it was working in 2.3.9. I'll upload a failing app as soon as I get a few minutes. :)
-
Matthew Daubert September 27th, 2010 @ 01:58 PM
Attached is a barebones app that fails for me (Rails 3.0.0, Ruby 1.9.2p0 x86_64-darwin10.4.0, OSX 10.6.4). The partial shared/messages has content_for(:script) but no content is rendered from the layout's yield(:script) statement.
-
Luca Guidi September 27th, 2010 @ 08:43 PM
IMO this isn't a bug, because AV renders the template first then the layout.
In your example:
1. The template is being rendered (mainyield
)
2. AV starts with the layout
2a. It rendersshared/head
, at this timecontent_for(:script)
wasn't triggered yet, that's becauseyield
doesn't print anything
2b. It rendersshared/messages
, now the content are initialized but the above partial was already rendered. -
Ryan Bigg September 27th, 2010 @ 11:15 PM
- State changed from new to wontfix
- Importance changed from to Low
Luca is correct.
If the
yield :blah
is processed before thecontent_for :blah
then theyield
will output nothing. -
Ryan Bigg September 27th, 2010 @ 11:15 PM
- Tag changed from rails3.0.0 to 3.0.0
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>