Skip to content

Commit

Permalink
Merge pull request #50466 from skipkayhil/hm-fix-to-symbol-raising-no…
Browse files Browse the repository at this point in the history
…methoderror

Add custom ArgumentError for invalid to: values
  • Loading branch information
rafaelfranca committed Jan 15, 2024
1 parent 16cd025 commit c1e8b36
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
21 changes: 10 additions & 11 deletions actionpack/lib/action_dispatch/routing/mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,16 @@ def normalize_options!(options, path_params, modyoule)
if to.respond_to?(:action) || to.respond_to?(:call)
options
else
to_endpoint = split_to to
controller = to_endpoint[0] || default_controller
action = to_endpoint[1] || default_action
if to.nil?
controller = default_controller
action = default_action
elsif to.is_a?(String) && to.include?("#")
to_endpoint = to.split("#").map!(&:-@)
controller = to_endpoint[0]
action = to_endpoint[1]
else
raise ArgumentError, ":to must respond to `action` or `call`, or it must be a String that includes '#'"
end

controller = add_controller_module(controller, modyoule)

Expand Down Expand Up @@ -308,14 +315,6 @@ def check_part(name, part, path_params, hash)
hash
end

def split_to(to)
if to&.include?("#")
to.split("#").map!(&:-@)
else
[]
end
end

def add_controller_module(controller, modyoule)
if modyoule && !controller.is_a?(Regexp)
if controller&.start_with?("/")
Expand Down
11 changes: 10 additions & 1 deletion actionpack/test/dispatch/routing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4054,7 +4054,16 @@ def test_missing_controller_with_to
get "/foo/bar", to: "foo"
end
}
assert_match(/Missing :controller/, ex.message)
assert_match(/:to must respond to/, ex.message)
end

def test_to_is_a_symbol
ex = assert_raises(ArgumentError) {
draw do
get "/foo/bar", to: :foo
end
}
assert_match(/:to must respond to/, ex.message)
end

def test_missing_action_on_hash
Expand Down

0 comments on commit c1e8b36

Please sign in to comment.