Skip to content

Commit

Permalink
Restore to: option with an implicit controller
Browse files Browse the repository at this point in the history
The `:to` option for routes can once again be a String without a
controller if the controller is implicitly provided by a nesting
`controller` or `resources` call.
  • Loading branch information
etiennebarrie committed Apr 10, 2024
1 parent 6e551d2 commit 36ff424
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
12 changes: 12 additions & 0 deletions actionpack/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
* Fix a regression in 7.1.3 passing a `to:` option without a controller when the controller is already defined by a scope.

```ruby
Rails.application.routes.draw do
controller :home do
get "recent", to: "recent_posts"
end
end
```

*Étienne Barrié*

* Request Forgery takes relative paths into account.

*Stefan Wienert*
Expand Down
15 changes: 10 additions & 5 deletions actionpack/lib/action_dispatch/routing/mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,17 @@ def normalize_options!(options, path_params, modyoule)
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]
elsif to.is_a?(String)
if to.include?("#")
to_endpoint = to.split("#").map!(&:-@)
controller = to_endpoint[0]
action = to_endpoint[1]
else
controller = default_controller
action = to
end
else
raise ArgumentError, ":to must respond to `action` or `call`, or it must be a String that includes '#'"
raise ArgumentError, ":to must respond to `action` or `call`, or it must be a String that includes '#', or the controller should be implicit"
end

controller = add_controller_module(controller, modyoule)
Expand Down
14 changes: 12 additions & 2 deletions actionpack/test/dispatch/routing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4037,6 +4037,7 @@ def draw(&block)
routes = ActionDispatch::Routing::RouteSet.new
routes.draw(&block)
@app = self.class.build_app routes
@routes = routes
end

def test_missing_controller
Expand All @@ -4054,7 +4055,16 @@ def test_missing_controller_with_to
get "/foo/bar", to: "foo"
end
}
assert_match(/:to must respond to/, ex.message)
assert_match(/Missing :controller/, ex.message)
end

def test_implicit_controller_with_to
draw do
controller :foo do
get "/foo/bar", to: "bar"
end
end
assert_routing "/foo/bar", controller: "foo", action: "bar"
end

def test_to_is_a_symbol
Expand All @@ -4066,7 +4076,7 @@ def test_to_is_a_symbol
assert_match(/:to must respond to/, ex.message)
end

def test_missing_action_on_hash
def test_missing_action_with_to
ex = assert_raises(ArgumentError) {
draw do
get "/foo/bar", to: "foo#"
Expand Down

0 comments on commit 36ff424

Please sign in to comment.