Skip to content

Commit

Permalink
Fix rails#5440 - multiple render_to_string breaks partials formats
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
drogus committed Mar 17, 2012
1 parent e135ff1 commit 1eb6189
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions actionpack/lib/abstract_controller/rendering.rb
Expand Up @@ -106,6 +106,7 @@ def render_to_body(options = {})
# 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

Expand Down
26 changes: 26 additions & 0 deletions actionpack/test/controller/render_test.rb
Expand Up @@ -549,6 +549,17 @@ def render_to_string_with_partial
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
Expand Down Expand Up @@ -1263,6 +1274,21 @@ def test_render_to_string_partial
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 "<strong>only partial</strong>\n", assigns(:html)
assert_equal "<strong>only html partial</strong>\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 "<strong>only partial</strong>\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
Expand Down
1 change: 1 addition & 0 deletions actionpack/test/fixtures/test/_partial_only_html.html
@@ -0,0 +1 @@
only html partial
1 change: 1 addition & 0 deletions actionpack/test/fixtures/test/with_html_partial.html.erb
@@ -0,0 +1 @@
<strong><%= render :partial => "partial_only_html" %></strong>
1 change: 1 addition & 0 deletions actionpack/test/fixtures/test/with_partial.html.erb
@@ -0,0 +1 @@
<strong><%= render :partial => "partial_only" %></strong>
1 change: 1 addition & 0 deletions actionpack/test/fixtures/test/with_partial.text.erb
@@ -0,0 +1 @@
**<%= render :partial => "partial_only" %>**

0 comments on commit 1eb6189

Please sign in to comment.