Skip to content

Commit 7266313

Browse files
authored
Merge pull request rails#50466 from skipkayhil/hm-fix-to-symbol-raising-nomethoderror
Add custom ArgumentError for invalid to: values
2 parents 3cae956 + a39332f commit 7266313

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

actionpack/lib/action_dispatch/routing/mapper.rb

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,16 @@ def normalize_options!(options, path_params, modyoule)
217217
if to.respond_to?(:action) || to.respond_to?(:call)
218218
options
219219
else
220-
to_endpoint = split_to to
221-
controller = to_endpoint[0] || default_controller
222-
action = to_endpoint[1] || default_action
220+
if to.nil?
221+
controller = default_controller
222+
action = default_action
223+
elsif to.is_a?(String) && to.include?("#")
224+
to_endpoint = to.split("#").map!(&:-@)
225+
controller = to_endpoint[0]
226+
action = to_endpoint[1]
227+
else
228+
raise ArgumentError, ":to must respond to `action` or `call`, or it must be a String that includes '#'"
229+
end
223230

224231
controller = add_controller_module(controller, modyoule)
225232

@@ -308,14 +315,6 @@ def check_part(name, part, path_params, hash)
308315
hash
309316
end
310317

311-
def split_to(to)
312-
if to&.include?("#")
313-
to.split("#").map!(&:-@)
314-
else
315-
[]
316-
end
317-
end
318-
319318
def add_controller_module(controller, modyoule)
320319
if modyoule && !controller.is_a?(Regexp)
321320
if controller&.start_with?("/")

actionpack/test/dispatch/routing_test.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4054,7 +4054,16 @@ def test_missing_controller_with_to
40544054
get "/foo/bar", to: "foo"
40554055
end
40564056
}
4057-
assert_match(/Missing :controller/, ex.message)
4057+
assert_match(/:to must respond to/, ex.message)
4058+
end
4059+
4060+
def test_to_is_a_symbol
4061+
ex = assert_raises(ArgumentError) {
4062+
draw do
4063+
get "/foo/bar", to: :foo
4064+
end
4065+
}
4066+
assert_match(/:to must respond to/, ex.message)
40584067
end
40594068

40604069
def test_missing_action_on_hash

0 commit comments

Comments
 (0)