mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Raise an error on root route naming conflicts.
Raises an ArgumentError when multiple root routes are defined in the same context instead of assigning nil names to subsequent roots.
This commit is contained in:
parent
023a840f5f
commit
dde9c48839
3 changed files with 23 additions and 8 deletions
|
@ -1,3 +1,10 @@
|
||||||
|
* Raise an error on root route naming conflicts.
|
||||||
|
|
||||||
|
Raises an ArgumentError when multiple root routes are defined in the
|
||||||
|
same context instead of assigning nil names to subsequent roots.
|
||||||
|
|
||||||
|
*Gannon McGibbon*
|
||||||
|
|
||||||
* Allow rescue from parameter parse errors:
|
* Allow rescue from parameter parse errors:
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
|
@ -656,7 +656,7 @@ module ActionDispatch
|
||||||
|
|
||||||
# Query if the following named route was already defined.
|
# Query if the following named route was already defined.
|
||||||
def has_named_route?(name)
|
def has_named_route?(name)
|
||||||
@set.named_routes.key? name
|
@set.named_routes.key?(name)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
@ -1952,9 +1952,7 @@ module ActionDispatch
|
||||||
end
|
end
|
||||||
|
|
||||||
def match_root_route(options)
|
def match_root_route(options)
|
||||||
name = has_named_route?(name_for_action(:root, nil)) ? nil : :root
|
args = ["/", { as: :root, via: :get }.merge(options)]
|
||||||
args = ["/", { as: name, via: :get }.merge!(options)]
|
|
||||||
|
|
||||||
match(*args)
|
match(*args)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -3698,15 +3698,25 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_multiple_roots
|
def test_multiple_roots_raises_error
|
||||||
draw do
|
ex = assert_raises(ArgumentError) {
|
||||||
namespace :foo do
|
draw do
|
||||||
root "pages#index", constraints: { host: "www.example.com" }
|
root "pages#index", constraints: { host: "www.example.com" }
|
||||||
root "admin/pages#index", constraints: { host: "admin.example.com" }
|
root "admin/pages#index", constraints: { host: "admin.example.com" }
|
||||||
end
|
end
|
||||||
|
}
|
||||||
|
assert_match(/Invalid route name, already in use: 'root'/, ex.message)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_multiple_named_roots
|
||||||
|
draw do
|
||||||
|
namespace :foo do
|
||||||
|
root "pages#index", constraints: { host: "www.example.com" }
|
||||||
|
root "admin/pages#index", constraints: { host: "admin.example.com" }, as: :admin_root
|
||||||
|
end
|
||||||
|
|
||||||
root "pages#index", constraints: { host: "www.example.com" }
|
root "pages#index", constraints: { host: "www.example.com" }
|
||||||
root "admin/pages#index", constraints: { host: "admin.example.com" }
|
root "admin/pages#index", constraints: { host: "admin.example.com" }, as: :admin_root
|
||||||
end
|
end
|
||||||
|
|
||||||
get "http://www.example.com/foo"
|
get "http://www.example.com/foo"
|
||||||
|
|
Loading…
Reference in a new issue