Skip to content
This repository
Browse code

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.
commit 1eb6189404f53ae59e4cb47ef9d5ad3a9bec3064 1 parent e135ff1
Piotr Sarnacki authored March 15, 2012
1  actionpack/lib/abstract_controller/rendering.rb
@@ -106,6 +106,7 @@ def render_to_body(options = {})
106 106
     # Find and renders a template based on the options given.
107 107
     # :api: private
108 108
     def _render_template(options) #:nodoc:
  109
+      lookup_context.rendered_format = nil if options[:formats]
109 110
       view_renderer.render(view_context, options)
110 111
     end
111 112
 
26  actionpack/test/controller/render_test.rb
@@ -549,6 +549,17 @@ def render_to_string_with_partial
549 549
     render :template => "test/hello_world"
550 550
   end
551 551
 
  552
+  def render_to_string_with_template_and_html_partial
  553
+    @text = render_to_string :template => "test/with_partial", :formats => [:text]
  554
+    @html = render_to_string :template => "test/with_partial", :formats => [:html]
  555
+    render :template => "test/with_html_partial"
  556
+  end
  557
+
  558
+  def render_to_string_and_render_with_different_formats
  559
+    @html = render_to_string :template => "test/with_partial", :formats => [:html]
  560
+    render :template => "test/with_partial", :formats => [:text]
  561
+  end
  562
+
552 563
   def partial_with_counter
553 564
     render :partial => "counter", :locals => { :counter_counter => 5 }
554 565
   end
@@ -1263,6 +1274,21 @@ def test_render_to_string_partial
1263 1274
     assert_equal "text/html", @response.content_type
1264 1275
   end
1265 1276
 
  1277
+  def test_render_to_string_with_template_and_html_partial
  1278
+    get :render_to_string_with_template_and_html_partial
  1279
+    assert_equal "**only partial**\n", assigns(:text)
  1280
+    assert_equal "<strong>only partial</strong>\n", assigns(:html)
  1281
+    assert_equal "<strong>only html partial</strong>\n", @response.body
  1282
+    assert_equal "text/html", @response.content_type
  1283
+  end
  1284
+
  1285
+  def test_render_to_string_and_render_with_different_formats
  1286
+    get :render_to_string_and_render_with_different_formats
  1287
+    assert_equal "<strong>only partial</strong>\n", assigns(:html)
  1288
+    assert_equal "**only partial**\n", @response.body
  1289
+    assert_equal "text/plain", @response.content_type
  1290
+  end
  1291
+
1266 1292
   def test_partial_with_counter
1267 1293
     get :partial_with_counter
1268 1294
     assert_equal "5", @response.body
1  actionpack/test/fixtures/test/_partial_only_html.html
... ...
@@ -0,0 +1 @@
  1
+only html partial
1  actionpack/test/fixtures/test/with_html_partial.html.erb
... ...
@@ -0,0 +1 @@
  1
+<strong><%= render :partial => "partial_only_html" %></strong>
1  actionpack/test/fixtures/test/with_partial.html.erb
... ...
@@ -0,0 +1 @@
  1
+<strong><%= render :partial => "partial_only" %></strong>
1  actionpack/test/fixtures/test/with_partial.text.erb
... ...
@@ -0,0 +1 @@
  1
+**<%= render :partial => "partial_only" %>**

0 notes on commit 1eb6189

Please sign in to comment.
Something went wrong with that request. Please try again.