Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #9209 (closed defect: fixed)

Opened 1 year ago

Last modified 6 months ago

[PATCH] Render partial with layout works incorrectly when rendered from site layout

Reported by: antramm Assigned to: core
Priority: normal Milestone:
Component: ActionPack Version: edge
Severity: normal Keywords: partial layout patch unverified
Cc:

Description

When rendering a partial (with a layout) in a main layout, the @content_for_layout rendered for the main page content is overwritten with what was rendered for the partial. e.g.

/layouts/application.html.erb

  <% render(:layout => "layout_for_partial", :locals => { :name => "Anthony" }) do %>Inside from first block in layout<% end %>
  <%= yield %>
  <% render(:layout => "layout_for_partial", :locals => { :name => "Ramm" }) do %>Inside from second block in layout<% end %>

/controller/action.html.erb

  <%= render(:layout => "layout_for_partial", :partial => "partial_for_use_in_layout", :locals => { :name => "David" }) %>

/controller/_ partial_for_use_in_layout

  Inside from partial (<%= name %>)

/controller/_layout_for_partial.html.erb

  Before (<%= name %>)
  <%= yield %>
  After

This should render as

  Before (Anthony)
  Inside from partial (Anthony)
  After
  Before (David)
  Inside from partial (David)
  After
  Before (Ramm)
  Inside from partial (Ramm)
  After

But actually renders as

  Before (Anthony)
  Inside from partial (Anthony)
  After
  Inside from partial (Anthony)
  Before (Ramm)
  Inside from partial (Ramm)
  After

This is because the content is rendered before the layout and stored in @content_for_layout, the layout is then rendered and when another

render :layout => 'blahblah', :partial => 'any_other_partial'

is run @content_for_layout is overwritten by the rendered result from this call. So when yield is called from the layout, the contents of this rendered partial are returned again.

This patch adds fixes this problems and adds tests to ensure it works correctly.

Attachments

render_partial_with_layout_patch.diff (4.9 kB) - added by antramm on 08/07/07 14:50:14.
Allows partials with a layout to be rendered from a main layout
render_partial_with_layout_patch_2.diff (4.9 kB) - added by antramm on 12/18/07 04:08:56.
New version of patch - tested against r8429

Change History

08/07/07 14:50:14 changed by antramm

  • attachment render_partial_with_layout_patch.diff added.

Allows partials with a layout to be rendered from a main layout

08/08/07 17:50:42 changed by antramm

  • summary changed from [PATCH] Render partial with layout works incorrectly when rendered both within content_for_layout and a wrapper layout to [PATCH] Render partial with layout works incorrectly when rendered from site layout.

09/24/07 17:23:58 changed by david

  • keywords changed from partial layout patch tested to partial layout patch unverified.
  • milestone deleted.

This currently fails on trunk with:

  1) Failure:
test_render_call_to_partial_with_layout_in_main_layout_and_within_content_for_layout(NewRenderTest)
    [./test/controller/new_render_test.rb:842:in `test_render_call_to_partial_with_layout_in_main_layout_and_within_content_for_layout'
     /usr/local/lib/ruby/gems/1.8/gems/mocha-0.5.5/lib/mocha/test_case_adapter.rb:19:in `__send__'
     /usr/local/lib/ruby/gems/1.8/gems/mocha-0.5.5/lib/mocha/test_case_adapter.rb:19:in `run']:
<"Before (Anthony)\nInside from partial (Anthony)\nAfter\nBefore (David)\nInside from partial (David)\nAfter\nBefore (Ramm)\nInside from partial (Ramm)\nAfter"> expected but was
<"Before (Anthony)\nInside from partial (Anthony)\nAfter\nBefore (David)\nInside from partial (David)\nAfter\nBefore (Ramm)\nInside from partial (Ramm)\nAfter\nBefore (Anthony)\nInside from partial (Anthony)\nAfter\nBefore (David)\nInside from partial (David)\nAfter\nBefore (Ramm)\nInside from partial (Ramm)\nAfter">.

  2) Failure:
test_using_layout_around_block_in_main_layout_and_within_content_for_layout(NewRenderTest)
    [./test/controller/new_render_test.rb:852:in `test_using_layout_around_block_in_main_layout_and_within_content_for_layout'
     /usr/local/lib/ruby/gems/1.8/gems/mocha-0.5.5/lib/mocha/test_case_adapter.rb:19:in `__send__'
     /usr/local/lib/ruby/gems/1.8/gems/mocha-0.5.5/lib/mocha/test_case_adapter.rb:19:in `run']:
<"Before (Anthony)\nInside from first block in layout\nAfter\nBefore (David)\nInside from block\nAfter\nBefore (Ramm)\nInside from second block in layout\nAfter\n"> expected but was
<"Before (Anthony)\nInside from first block in layout\nAfter\nBefore (David)\nInside from block\nAfter\nBefore (Ramm)\nInside from second block in layout\nAfter\nBefore (Anthony)\nInside from first block in layout\nAfter\nBefore (David)\nInside from block\nAfter\nBefore (Ramm)\nInside from second block in layout\nAfter\n">.

10/17/07 11:06:05 changed by antramm

Works for me on edge r7952:-

Loaded suite actionpack/test/controller/new_render_test Started .......................................................................... Finished in 0.182891 seconds.

74 tests, 249 assertions, 0 failures, 0 errors

12/14/07 00:49:15 changed by mdemare

+1

12/15/07 00:00:46 changed by david

I get:

patching file actionpack/test/controller/new_render_test.rb
Hunk #1 succeeded at 361 (offset 8 lines).
Hunk #2 succeeded at 395 (offset 8 lines).
Hunk #3 FAILED at 827.
1 out of 3 hunks FAILED -- saving rejects to file actionpack/test/controller/new_render_test.rb.rej
patching file actionpack/test/fixtures/layouts/block_with_layout.erb
patching file actionpack/test/fixtures/layouts/partial_with_layout.erb
patching file actionpack/lib/action_view/base.rb
Hunk #1 succeeded at 338 (offset 30 lines).
Hunk #2 succeeded at 443 (offset -2 lines).

Trying to apply to trunk now.

12/18/07 04:08:56 changed by antramm

  • attachment render_partial_with_layout_patch_2.diff added.

New version of patch - tested against r8429

12/18/07 04:11:30 changed by antramm

Added updated patch Currently passes tests against r8431.

01/03/08 15:28:52 changed by david

  • status changed from new to closed.
  • resolution set to fixed.

(In [8541]) Fixed rendering of partials with layout when done from site layout (closes #9209) [antramm]