Skip to content

Commit b272be8

Browse files
authored
Merge pull request rails#51523 from etiennebarrie/route-to-implicit-controller
Restore `to:` option in routes with an implicit controller
2 parents 57cbc2e + 36ff424 commit b272be8

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

actionpack/CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
* Fix a regression in 7.1.3 passing a `to:` option without a controller when the controller is already defined by a scope.
2+
3+
```ruby
4+
Rails.application.routes.draw do
5+
controller :home do
6+
get "recent", to: "recent_posts"
7+
end
8+
end
9+
```
10+
11+
*Étienne Barrié*
12+
113
* Request Forgery takes relative paths into account.
214

315
*Stefan Wienert*

actionpack/lib/action_dispatch/routing/mapper.rb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,12 +231,17 @@ def normalize_options!(options, path_params, modyoule)
231231
if to.nil?
232232
controller = default_controller
233233
action = default_action
234-
elsif to.is_a?(String) && to.include?("#")
235-
to_endpoint = to.split("#").map!(&:-@)
236-
controller = to_endpoint[0]
237-
action = to_endpoint[1]
234+
elsif to.is_a?(String)
235+
if to.include?("#")
236+
to_endpoint = to.split("#").map!(&:-@)
237+
controller = to_endpoint[0]
238+
action = to_endpoint[1]
239+
else
240+
controller = default_controller
241+
action = to
242+
end
238243
else
239-
raise ArgumentError, ":to must respond to `action` or `call`, or it must be a String that includes '#'"
244+
raise ArgumentError, ":to must respond to `action` or `call`, or it must be a String that includes '#', or the controller should be implicit"
240245
end
241246

242247
controller = add_controller_module(controller, modyoule)

actionpack/test/dispatch/routing_test.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4037,6 +4037,7 @@ def draw(&block)
40374037
routes = ActionDispatch::Routing::RouteSet.new
40384038
routes.draw(&block)
40394039
@app = self.class.build_app routes
4040+
@routes = routes
40404041
end
40414042

40424043
def test_missing_controller
@@ -4054,7 +4055,16 @@ def test_missing_controller_with_to
40544055
get "/foo/bar", to: "foo"
40554056
end
40564057
}
4057-
assert_match(/:to must respond to/, ex.message)
4058+
assert_match(/Missing :controller/, ex.message)
4059+
end
4060+
4061+
def test_implicit_controller_with_to
4062+
draw do
4063+
controller :foo do
4064+
get "/foo/bar", to: "bar"
4065+
end
4066+
end
4067+
assert_routing "/foo/bar", controller: "foo", action: "bar"
40584068
end
40594069

40604070
def test_to_is_a_symbol
@@ -4066,7 +4076,7 @@ def test_to_is_a_symbol
40664076
assert_match(/:to must respond to/, ex.message)
40674077
end
40684078

4069-
def test_missing_action_on_hash
4079+
def test_missing_action_with_to
40704080
ex = assert_raises(ArgumentError) {
40714081
draw do
40724082
get "/foo/bar", to: "foo#"

0 commit comments

Comments
 (0)