Skip to content

Commit 3b8222c

Browse files
authored
Merge pull request rails#51023 from Shopify/av-non-string-template
Allow template to return any kind of objects
2 parents f5ea4a5 + f62ff52 commit 3b8222c

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

actionview/lib/action_view/template.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,8 @@ def render(view, locals, buffer = nil, implicit_locals: [], add_to_stack: true,
272272
view._run(method_name, self, locals, buffer, add_to_stack: add_to_stack, has_strict_locals: strict_locals?, &block)
273273
nil
274274
else
275-
view._run(method_name, self, locals, OutputBuffer.new, add_to_stack: add_to_stack, has_strict_locals: strict_locals?, &block)&.to_s
275+
result = view._run(method_name, self, locals, OutputBuffer.new, add_to_stack: add_to_stack, has_strict_locals: strict_locals?, &block)
276+
result.is_a?(OutputBuffer) ? result.to_s : result
276277
end
277278
end
278279
rescue => e

actionview/test/template/template_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,14 @@ def test_virtual_path
121121
assert_equal "hellopartialhello", render
122122
end
123123

124+
def test_rendering_non_string
125+
my_object = Object.new
126+
eval_handler = ->(_template, source) { source }
127+
@template = ActionView::Template.new("my_object", "__id__", eval_handler, virtual_path: "hello", locals: [:my_object])
128+
result = render(my_object: my_object)
129+
assert_same my_object, result
130+
end
131+
124132
def test_resulting_string_is_utf8
125133
@template = new_template
126134
assert_equal Encoding::UTF_8, render.encoding

0 commit comments

Comments
 (0)