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(:inline) in a layout before yield replaces original content

#4777

The issue I'm seeing is that if you call render(:inline => "some text") in the layout before yielding to the main content for the page, the content of the yield will replace the original content of the page.

Perhaps this is the intended behavior and I just don't understand. I haven't delved into the source yet to see why this is happening, or if using render :inline in a layout is in fact incorrect usage.

Nor have put together tests or a demo to exhibit it, but it's fairly simple to reproduce.

Given a fresh project with a controller 'test' and an #index action.

rails g controller test index

In config/routes.rb

root :to => "test#index"

In app/views/layouts/application.html.erb

<%= render :inline => "layout inline 1" %>
<%= yield %>
<%= render :inline => "layout inline 2" %>

If you start up the server and hit the home page you'll see:

layout inline 1
layout inline 1
layout inline 2

The content in index.html.erb has been replaced by the inline render prior. This does not happen in Rails 2.

Reported by Travis · June 6th, 2010 @ 04:52 PM

State: resolved
Milestone: 3.0.2
Assigned to: José Valim José Valim
Importance: High

Activity

  1. Rohit Arondekar
    Rohit Arondekar
    • Tag set to rails 3, actionview, render
    • Assigned user set to José Valim

    Confirmed on Rails master, Ruby 1.9.2-head.

    According to the edge api docs => http://edgeapi.rubyonrails.org/classes/ActionView/Rendering.html#M0...

    :inline - Renders an inline template similar to how it‘s done in the controller.

    June 11th, 2010 @ 10:49 AM

  2. José Valim
  3. Neeraj Singh
    Neeraj Singh
    • Tag changed from rails 3, actionview, render to rails 3, actionview, patch, render

    Attached is patch with test.

    June 11th, 2010 @ 06:45 PM

  4. Rohit Arondekar
    Rohit Arondekar

    +1 applies cleanly, tests pass on the following Rubies:

    • ruby 1.8.7 (2009-06-12 patchlevel 174)
    • ruby 1.9.2dev (2010-06-06 revision 28184)

    June 12th, 2010 @ 02:20 AM

  5. Paul Barry
    Paul Barry

    +1, applies cleanly, resolves the issue

    June 12th, 2010 @ 03:40 AM

  6. José Valim
    José Valim

    The tests may pass, but it's not the correct fix because we should not use the identifier for the purpose above and it's a special check for a given template type. I will discuss with Yehuda a bit more and follow up soon, but a more acceptable solution would be to only set the layout if a layout object exists:

    @_content_for[:layout] = content if layout (only set the content for layout if there is a layout)
    

    Neeraj, could you try this solution and see if all tests still pass?

    June 12th, 2010 @ 06:44 AM

  7. Neeraj Singh
    Neeraj Singh

    Attached is modified patch (and same test) based on feedback by José .

    June 13th, 2010 @ 04:04 AM

  8. Rohit Arondekar
    Rohit Arondekar

    Applies cleanly and the tests pass on 1.8.7 and 1.9.2-head.

    June 13th, 2010 @ 04:55 AM

  9. Repository
  10. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 3.0.2
    • Importance changed from to High

    October 15th, 2010 @ 11:01 PM