Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom ArgumentError for invalid to: values #50466

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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