Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

render :layout with block and multiple yields renders twice

#2279

Its possible I'm misusing render :layout for sharing view markup between layouts, but this seems strange nonetheless.


<!-- layouts/_common.html.erb -->
common
<%= yield :top %>
<%= yield %>

<!-- layouts/application.html.erb -->
<% render :layout => 'layouts/common' do %>
  layout
  <%= yield :layout %>
<% end %>

<!-- some view -->
view

outputs (newlines added):

common
layout   <-- Added by yield :top
view
layout   <-- Added by yield
view

Notice, I haven't declared content_for :top, but it is outputting an extra 'layout view'.

Adding any yields to common with an argument, like 'yield :bottom', also output the extra 'layout view'.

Note: In application.html.erb, I tried just putting yield, not yield :layout, but I got a stack level too deep error.

I reworked my shared layouts to not pass a block to render :layout, but thought I would see if this was in fact a bug.

Reported by Jeff Schuil · March 17th, 2009 @ 10:12 PM

State: stale
Milestone: 2.3.10
Assigned to: Santiago Pastorino Santiago Pastorino
Importance: Medium

Activity

  1. Jeff Schuil
    Jeff Schuil

    Also, in _common.html.erb, if instead of <%= yield :top %> I use (the deprecated) <%= @content_for_top %> it works as expected.

    March 17th, 2009 @ 10:35 PM

  2. Steve Madsen
    Steve Madsen
    • Tag changed from content_for, layout, render, yield to 2.3.5, content_for, layout, render, yield

    I don't see a doubled "view", but I can confirm that I reproduced the "stack too deep" problem when rendering a complex template on 2.3.5. In my case, the template rendered a block with a layout, and inside the block, rendered a partial with a different layout, the whole thing wrapped in a normal layout. Yes, hairy. Inherited code, one of many things I'm trying to clean up.

    Specifying "yield :layout" explicitly in every template that acts as a layout fixed the recursion that happens due to the render-block-with-layout code. The problem also goes away if I remove the render-block-with-layout (keeping the inner part), though in that case part of the page design goes away, so it's not a very good fix.

    December 16th, 2009 @ 03:03 AM

  3. Leandro Pedroni
    Leandro Pedroni

    There's also a problem with passing a layout to render a collection with partials.
    The layout is applied around the whole collection (as expected), but the whole thing (layout plus collection partials) gets rendered as many times as the items in the collection.

    Here's a quick patch that should fix both problems...

    February 3rd, 2010 @ 03:38 PM

  4. Rasmus Rønn Nielsen
    Rasmus Rønn Nielsen

    I've run into this problem running ruby1.9.1 and rails 3 beta. I'm trying to use two "render :layout do [something] end" inside each other.

    February 23rd, 2010 @ 09:38 AM

  5. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  6. Aditya
    Aditya
    • Importance changed from to

    Confirm that this is a problem on 2.3.8 as well.

    Leandro's patch fixes the problem of repeated output when passing collection is used problem.
    Render layout with block however is not working (with or without the patch above).

    Ruby -v
    ruby 1.8.7 (2010-06-07 patchlevel 268)

    June 27th, 2010 @ 08:55 PM

  7. Santiago Pastorino
    Santiago Pastorino
    • Tag changed from 2.3.5, content_for, layout, render, yield to content_for, layout, render, yield
    • Assigned user set to Santiago Pastorino
    • State changed from new to open
    • Milestone cleared.
    • Importance changed from to Medium

    Leandro can you provide a patch following http://rails.lighthouseapp.com/projects/8994/sending-patches and add a test for this cases?.
    Thank you.

    June 30th, 2010 @ 01:59 AM

  8. Neeraj Singh
    Neeraj Singh

    I am not able to reproduce this problem in 2-3-stable. However Aditya was able to recreate this problem just 3 days ago. Not sure what's going on.

    Can someone else try?

    July 1st, 2010 @ 03:44 AM

  9. masone
    masone

    I can reproduce the output described by Jeff with Rails 2.3.8, Ruby 1.8.7 and 1.9.2.

    July 18th, 2010 @ 12:06 PM

  10. Santiago Pastorino
    Santiago Pastorino

    Is this an issue on master too? or only 2-3-stable?

    July 19th, 2010 @ 04:37 PM

  11. Rohit Arondekar
    Rohit Arondekar
    • Milestone set to 2.3.9

    I can't reproduce this on 3-0-stable. Changing milestone.

    Can somebody please try this in 2-3-stable?

    August 3rd, 2010 @ 08:41 AM

  12. Santiago Pastorino
    Santiago Pastorino

    masone has reproduced this on 2.3.8 on July 18th, can you try it on 2-3-stable from git?.
    And can someone provide a patch

    August 3rd, 2010 @ 04:42 PM

  13. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.3.9 to 2.3.10

    August 30th, 2010 @ 02:28 AM

  14. jgeiger
    jgeiger

    I've created a sample app that may or may not be this issue but is very similar. I'm not sure that the yields are causing the failures.

    http://github.com/jgeiger/double_render_bug

    September 27th, 2010 @ 06:51 PM

  15. Shimon Young
    Shimon Young

    Isn't this because actionpack-3.0.0\lib\action_view\render\rendering.rb is:

    def render(options = {}, locals = {}, &block)
      case options
      when Hash
        if block_given?
          _render_partial(options.merge(:partial => options[:layout]), &block)
    

    where it should be?

    def render(options = {}, locals = {}, &block)
      case options
      when Hash
        if block_given?
          options.merge!(:partial => options[:layout])
          options.delete(:layout)
          _render_partial(options, &block)
    

    October 6th, 2010 @ 11:08 AM

  16. Santiago Pastorino
    Santiago Pastorino

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 2nd, 2011 @ 04:58 PM

  17. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 04:58 PM