From 1eb6189404f53ae59e4cb47ef9d5ad3a9bec3064 Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Thu, 15 Mar 2012 03:21:10 +0100 Subject: Fix #5440 - multiple render_to_string breaks partials formats This fixes situation where rendering template to string sets `rendered_format` to the format rendered there. This is ok to have consistent formats rendered in partials, but it breaks on next renders if format is explicitly set or on last render where default format does not necessarily need to be the format of first rendered template. --- actionpack/lib/abstract_controller/rendering.rb | 1 + actionpack/test/controller/render_test.rb | 26 ++++++++++++++++++++++ .../test/fixtures/test/_partial_only_html.html | 1 + .../test/fixtures/test/with_html_partial.html.erb | 1 + .../test/fixtures/test/with_partial.html.erb | 1 + .../test/fixtures/test/with_partial.text.erb | 1 + 6 files changed, 31 insertions(+) create mode 100644 actionpack/test/fixtures/test/_partial_only_html.html create mode 100644 actionpack/test/fixtures/test/with_html_partial.html.erb create mode 100644 actionpack/test/fixtures/test/with_partial.html.erb create mode 100644 actionpack/test/fixtures/test/with_partial.text.erb diff --git a/actionpack/lib/abstract_controller/rendering.rb b/actionpack/lib/abstract_controller/rendering.rb index 9019c07bca..f74fd6ac4a 100644 --- a/actionpack/lib/abstract_controller/rendering.rb +++ b/actionpack/lib/abstract_controller/rendering.rb @@ -106,6 +106,7 @@ module AbstractController # Find and renders a template based on the options given. # :api: private def _render_template(options) #:nodoc: + lookup_context.rendered_format = nil if options[:formats] view_renderer.render(view_context, options) end diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb index e9b0fe7008..daa2676e22 100644 --- a/actionpack/test/controller/render_test.rb +++ b/actionpack/test/controller/render_test.rb @@ -549,6 +549,17 @@ class TestController < ActionController::Base render :template => "test/hello_world" end + def render_to_string_with_template_and_html_partial + @text = render_to_string :template => "test/with_partial", :formats => [:text] + @html = render_to_string :template => "test/with_partial", :formats => [:html] + render :template => "test/with_html_partial" + end + + def render_to_string_and_render_with_different_formats + @html = render_to_string :template => "test/with_partial", :formats => [:html] + render :template => "test/with_partial", :formats => [:text] + end + def partial_with_counter render :partial => "counter", :locals => { :counter_counter => 5 } end @@ -1263,6 +1274,21 @@ class RenderTest < ActionController::TestCase assert_equal "text/html", @response.content_type end + def test_render_to_string_with_template_and_html_partial + get :render_to_string_with_template_and_html_partial + assert_equal "**only partial**\n", assigns(:text) + assert_equal "only partial\n", assigns(:html) + assert_equal "only html partial\n", @response.body + assert_equal "text/html", @response.content_type + end + + def test_render_to_string_and_render_with_different_formats + get :render_to_string_and_render_with_different_formats + assert_equal "only partial\n", assigns(:html) + assert_equal "**only partial**\n", @response.body + assert_equal "text/plain", @response.content_type + end + def test_partial_with_counter get :partial_with_counter assert_equal "5", @response.body diff --git a/actionpack/test/fixtures/test/_partial_only_html.html b/actionpack/test/fixtures/test/_partial_only_html.html new file mode 100644 index 0000000000..d2d630bd40 --- /dev/null +++ b/actionpack/test/fixtures/test/_partial_only_html.html @@ -0,0 +1 @@ +only html partial \ No newline at end of file diff --git a/actionpack/test/fixtures/test/with_html_partial.html.erb b/actionpack/test/fixtures/test/with_html_partial.html.erb new file mode 100644 index 0000000000..d84d909d64 --- /dev/null +++ b/actionpack/test/fixtures/test/with_html_partial.html.erb @@ -0,0 +1 @@ +<%= render :partial => "partial_only_html" %> diff --git a/actionpack/test/fixtures/test/with_partial.html.erb b/actionpack/test/fixtures/test/with_partial.html.erb new file mode 100644 index 0000000000..7502364cf5 --- /dev/null +++ b/actionpack/test/fixtures/test/with_partial.html.erb @@ -0,0 +1 @@ +<%= render :partial => "partial_only" %> diff --git a/actionpack/test/fixtures/test/with_partial.text.erb b/actionpack/test/fixtures/test/with_partial.text.erb new file mode 100644 index 0000000000..5f068ebf27 --- /dev/null +++ b/actionpack/test/fixtures/test/with_partial.text.erb @@ -0,0 +1 @@ +**<%= render :partial => "partial_only" %>** -- cgit v1.2.3 From 7130f9159146776067d334b5043b789860b92761 Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Thu, 15 Mar 2012 23:39:25 +0100 Subject: Add missing test for #5308 --- actionpack/test/controller/render_test.rb | 13 +++++++++++++ actionpack/test/fixtures/layouts/with_html_partial.html.erb | 1 + actionpack/test/fixtures/test/with_xml_template.html.erb | 1 + 3 files changed, 15 insertions(+) create mode 100644 actionpack/test/fixtures/layouts/with_html_partial.html.erb create mode 100644 actionpack/test/fixtures/test/with_xml_template.html.erb diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb index daa2676e22..3cc8a9ddf3 100644 --- a/actionpack/test/controller/render_test.rb +++ b/actionpack/test/controller/render_test.rb @@ -560,6 +560,12 @@ class TestController < ActionController::Base render :template => "test/with_partial", :formats => [:text] end + def render_template_within_a_template_with_other_format + render :template => "test/with_xml_template", + :formats => [:html], + :layout => "with_html_partial" + end + def partial_with_counter render :partial => "counter", :locals => { :counter_counter => 5 } end @@ -1289,6 +1295,13 @@ class RenderTest < ActionController::TestCase assert_equal "text/plain", @response.content_type end + def test_render_template_within_a_template_with_other_format + get :render_template_within_a_template_with_other_format + expected = "only html partial

This is grand!

" + assert_equal expected, @response.body.strip + assert_equal "text/html", @response.content_type + end + def test_partial_with_counter get :partial_with_counter assert_equal "5", @response.body diff --git a/actionpack/test/fixtures/layouts/with_html_partial.html.erb b/actionpack/test/fixtures/layouts/with_html_partial.html.erb new file mode 100644 index 0000000000..fd2896aeaa --- /dev/null +++ b/actionpack/test/fixtures/layouts/with_html_partial.html.erb @@ -0,0 +1 @@ +<%= render :partial => "partial_only_html" %><%= yield %> diff --git a/actionpack/test/fixtures/test/with_xml_template.html.erb b/actionpack/test/fixtures/test/with_xml_template.html.erb new file mode 100644 index 0000000000..e54a7cd001 --- /dev/null +++ b/actionpack/test/fixtures/test/with_xml_template.html.erb @@ -0,0 +1 @@ +<%= render :template => "test/greeting", :formats => :xml %> -- cgit v1.2.3