Skip to content

Commit

Permalink
If partial is rendered in controller, grab format from template
Browse files Browse the repository at this point in the history
Previously `rendered_format` was set only based on mime types
passed in Accept header, which was wrong if first type from
Accept was different than rendered partial. The fix is to simply
move setting rendered_format to the place where template
is available and grab format from the template. If it fails
we can fallback to formats passed by Accept header.
  • Loading branch information
drogus committed Mar 27, 2012
1 parent 3eb5be6 commit 449a4fc
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
10 changes: 8 additions & 2 deletions actionpack/lib/action_view/renderer/partial_renderer.rb
Expand Up @@ -221,6 +221,14 @@ def render(context, options, block)
setup(context, options, block)
identifier = (@template = find_partial) ? @template.identifier : @path

@lookup_context.rendered_format ||= begin
if @template && @template.formats.present?
@template.formats.first
else
formats.first
end
end

if @collection
instrument(:collection, :identifier => identifier || "collection", :count => @collection.size) do
render_collection
Expand Down Expand Up @@ -273,8 +281,6 @@ def setup(context, options, block)
@block = block
@details = extract_details(options)

@lookup_context.rendered_format ||= formats.first

if String === partial
@object = options[:object]
@path = partial
Expand Down
13 changes: 13 additions & 0 deletions actionpack/test/controller/render_test.rb
Expand Up @@ -543,6 +543,10 @@ def partial
render :partial => 'partial'
end

def partial_html_erb
render :partial => 'partial_html_erb'
end

def render_to_string_with_partial
@partial_only = render_to_string :partial => "partial_only"
@partial_with_locals = render_to_string :partial => "customer", :locals => { :customer => Customer.new("david") }
Expand Down Expand Up @@ -1267,6 +1271,15 @@ def test_should_render_html_formatted_partial
assert_equal "text/html", @response.content_type
end

def test_render_html_formatted_partial_even_with_other_mime_time_in_accept
@request.accept = "text/javascript, text/html"

get :partial_html_erb

assert_equal "partial.html.erb", @response.body.strip
assert_equal "text/html", @response.content_type
end

def test_should_render_html_partial_with_formats
get :partial_formats_html
assert_equal "partial html", @response.body
Expand Down
1 change: 1 addition & 0 deletions actionpack/test/fixtures/test/_partial_html_erb.html.erb
@@ -0,0 +1 @@
<%= "partial.html.erb" %>

0 comments on commit 449a4fc

Please sign in to comment.