Skip to content

Commit 5a61561

Browse files
committed
Merge pull request #11085 from acapilleri/partial_exception_message
fix identifier error message of partial with unvalid charter Conflicts: actionview/CHANGELOG.md Conflicts: actionview/CHANGELOG.md
1 parent 6e1bc45 commit 5a61561

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

actionpack/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
* Added an explicit error message, in `ActionView::PartialRenderer`
2+
for partial `rendering`, when the value of option `as` has invalid charters.
3+
4+
*Angelo Capilleri*
5+
16
* Restore handling of a bare `Authorization` header, without `token=`
27
prefix.
38

actionpack/lib/action_view/renderer/partial_renderer.rb

+10-2
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ def setup(context, options, block)
347347
end
348348

349349
if as = options[:as]
350-
raise_invalid_identifier(as) unless as.to_s =~ /\A[a-z_]\w*\z/
350+
raise_invalid_option_as(as) unless as.to_s =~ /\A[a-z_]\w*\z/
351351
as = as.to_sym
352352
end
353353

@@ -482,11 +482,19 @@ def retrieve_variable(path, as)
482482
end
483483

484484
IDENTIFIER_ERROR_MESSAGE = "The partial name (%s) is not a valid Ruby identifier; " +
485-
"make sure your partial name starts with a lowercase letter or underscore, " +
485+
"make sure your partial name starts with underscore, " +
486+
"and is followed by any combination of letters, numbers and underscores."
487+
488+
OPTION_AS_ERROR_MESSAGE = "The value (%s) of the option `as` is not a valid Ruby identifier; " +
489+
"make sure it starts with lowercase letter, " +
486490
"and is followed by any combination of letters, numbers and underscores."
487491

488492
def raise_invalid_identifier(path)
489493
raise ArgumentError.new(IDENTIFIER_ERROR_MESSAGE % (path))
490494
end
495+
496+
def raise_invalid_option_as(as)
497+
raise ArgumentError.new(OPTION_AS_ERROR_MESSAGE % (as))
498+
end
491499
end
492500
end

actionpack/test/template/render_test.rb

+17-3
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,14 @@ def test_render_partial_with_locals_from_default
176176
def test_render_partial_with_invalid_name
177177
e = assert_raises(ArgumentError) { @view.render(:partial => "test/200") }
178178
assert_equal "The partial name (test/200) is not a valid Ruby identifier; " +
179-
"make sure your partial name starts with a lowercase letter or underscore, " +
179+
"make sure your partial name starts with underscore, " +
180180
"and is followed by any combination of letters, numbers and underscores.", e.message
181181
end
182182

183183
def test_render_partial_with_missing_filename
184184
e = assert_raises(ArgumentError) { @view.render(:partial => "test/") }
185185
assert_equal "The partial name (test/) is not a valid Ruby identifier; " +
186-
"make sure your partial name starts with a lowercase letter or underscore, " +
186+
"make sure your partial name starts with underscore, " +
187187
"and is followed by any combination of letters, numbers and underscores.", e.message
188188
end
189189

@@ -195,7 +195,21 @@ def test_render_partial_with_incompatible_object
195195
def test_render_partial_with_hyphen
196196
e = assert_raises(ArgumentError) { @view.render(:partial => "test/a-in") }
197197
assert_equal "The partial name (test/a-in) is not a valid Ruby identifier; " +
198-
"make sure your partial name starts with a lowercase letter or underscore, " +
198+
"make sure your partial name starts with underscore, " +
199+
"and is followed by any combination of letters, numbers and underscores.", e.message
200+
end
201+
202+
def test_render_partial_with_invalid_option_as
203+
e = assert_raises(ArgumentError) { @view.render(:partial => "test/partial_only", :as => 'a-in') }
204+
assert_equal "The value (a-in) of the option `as` is not a valid Ruby identifier; " +
205+
"make sure it starts with lowercase letter, " +
206+
"and is followed by any combination of letters, numbers and underscores.", e.message
207+
end
208+
209+
def test_render_partial_with_hyphen_and_invalid_option_as
210+
e = assert_raises(ArgumentError) { @view.render(:partial => "test/a-in", :as => 'a-in') }
211+
assert_equal "The value (a-in) of the option `as` is not a valid Ruby identifier; " +
212+
"make sure it starts with lowercase letter, " +
199213
"and is followed by any combination of letters, numbers and underscores.", e.message
200214
end
201215

0 commit comments

Comments
 (0)