Skip to content

Commit 1ed80b0

Browse files
georgeclaghornDavid Heinemeier Hansson
authored andcommitted
Fix implicitly rendering a JSON partial with the same name as an HTML partial (#400)
1 parent 6627fd2 commit 1ed80b0

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

lib/jbuilder/jbuilder_template.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def _set_inline_partial(name, object, options)
188188
_scope{ _render_partial_with_options options.merge(collection: object) }
189189
else
190190
locals = ::Hash[options[:as], object]
191-
_scope{ _render_partial options.merge(locals: locals) }
191+
_scope{ _render_partial_with_options options.merge(locals: locals) }
192192
end
193193

194194
set! name, value

test/jbuilder_template_test.rb

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ class JbuilderTemplateTest < ActionView::TestCase
6262
Rails.cache.clear
6363
end
6464

65-
def jbuild(source)
65+
def jbuild(source, options = {})
6666
@rendered = []
67-
partials = PARTIALS.clone
67+
partials = options.fetch(:partials, PARTIALS).clone
6868
partials["test.json.jbuilder"] = source
6969
resolver = ActionView::FixtureResolver.new(partials)
7070
lookup_context.view_paths = [resolver]
@@ -438,4 +438,22 @@ def assert_collection_rendered(result, context = nil)
438438
assert_equal 123, result["id"]
439439
assert_equal "Chris Harris", result["name"]
440440
end
441+
442+
if Rails::VERSION::MAJOR >= 4
443+
test "renders partial via set! with same name as HTML partial" do
444+
partials = {
445+
"_blog_post.html.erb" => "Hello!",
446+
"_blog_post.json.jbuilder" => BLOG_POST_PARTIAL
447+
}
448+
449+
@post = BLOG_POST_COLLECTION.first
450+
451+
result = jbuild(<<-JBUILDER, partials: partials)
452+
json.post @post, partial: "blog_post", as: :blog_post
453+
JBUILDER
454+
455+
assert_not_nil result["post"]
456+
assert_equal 1, result["post"]["id"]
457+
end
458+
end
441459
end

0 commit comments

Comments
 (0)