Skip to content

Commit bc04455

Browse files
committed
Merge pull request rails#3695 from tobiassvn/partial_path_error
Meaningful errors for unexpected partial arguments. Fixes rails#3573
2 parents fec85cf + 771635e commit bc04455

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

actionpack/lib/action_view/renderer/partial_renderer.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,13 @@ def partial_path(object = @object)
366366
path = if object.respond_to?(:to_partial_path)
367367
object.to_partial_path
368368
else
369-
ActiveSupport::Deprecation.warn "ActiveModel-compatible objects whose classes return a #model_name that responds to #partial_path are deprecated. Please respond to #to_partial_path directly instead."
370-
object.class.model_name.partial_path
369+
klass = object.class
370+
if klass.respond_to?(:model_name)
371+
ActiveSupport::Deprecation.warn "ActiveModel-compatible objects whose classes return a #model_name that responds to #partial_path are deprecated. Please respond to #to_partial_path directly instead."
372+
klass.model_name.partial_path
373+
else
374+
raise ArgumentError.new("'#{object.inspect}' is not an ActiveModel-compatible object that returns a valid partial path.")
375+
end
371376
end
372377

373378
@partial_names[path] ||= path.dup.tap do |object_path|

actionpack/test/template/render_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,13 @@ def test_render_partial_with_invalid_name
142142
"and is followed by any combinations of letters, numbers, or underscores.", e.message
143143
end
144144

145+
def test_render_partial_with_incompatible_object
146+
@view.render(:partial => nil)
147+
flunk "Render did not raise ArgumentError"
148+
rescue ArgumentError => e
149+
assert_equal "'#{nil.inspect}' is not an ActiveModel-compatible object that returns a valid partial path.", e.message
150+
end
151+
145152
def test_render_partial_with_errors
146153
@view.render(:partial => "test/raise")
147154
flunk "Render did not raise Template::Error"

0 commit comments

Comments
 (0)