From 2ea66fc6c5728bdd51b164be25c43c8cbce396a5 Mon Sep 17 00:00:00 2001 From: Ryo Hashimoto Date: Mon, 29 Aug 2016 16:24:51 +0100 Subject: Fix nested multiple roots The PR #20940 enabled the use of multiple roots with different constraints at the top level but unfortunately didn't work when those roots were inside a namespace and also broke the use of root inside a namespace after a top level root was defined because the check for the existence of the named route used the global :root name and not the namespaced name. This is fixed by using the name_for_action method to expand the :root name to the full namespaced name. We can pass nil for the second argument as we're not dealing with resource definitions so don't need to handle the cases for edit and new routes. Fixes #26148. --- actionpack/CHANGELOG.md | 19 ++++++++++- actionpack/lib/action_dispatch/routing/mapper.rb | 2 +- actionpack/test/dispatch/routing_test.rb | 42 ++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 2 deletions(-) diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index a66a1e8af3..999ac82d42 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,10 +1,27 @@ +* Fix nested multiple roots + + The PR #20940 enabled the use of multiple roots with different constraints + at the top level but unfortunately didn't work when those roots were inside + a namespace and also broke the use of root inside a namespace after a top + level root was defined because the check for the existence of the named route + used the global :root name and not the namespaced name. + + This is fixed by using the name_for_action method to expand the :root name to + the full namespaced name. We can pass nil for the second argument as we're not + dealing with resource definitions so don't need to handle the cases for edit + and new routes. + + Fixes #26148. + + *Ryo Hashimoto*, *Andrew White* + * Include the content of the flash in the auto-generated etag. This solves the following problem: 1. POST /messages 2. redirect_to messages_url, notice: 'Message was created' 3. GET /messages/1 4. GET /messages - + Step 4 would before still include the flash message, even though it's no longer relevant, because the etag cache was recorded with the flash in place and didn't change when it was gone. diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index b3acac42fc..67b9463a6a 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -1927,7 +1927,7 @@ to this: end def match_root_route(options) - name = has_named_route?(:root) ? nil : :root + name = has_named_route?(name_for_action(:root, nil)) ? nil : :root args = ["/", { as: name, via: :get }.merge!(options)] match(*args) diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb index ee5c30ef0e..0938460632 100644 --- a/actionpack/test/dispatch/routing_test.rb +++ b/actionpack/test/dispatch/routing_test.rb @@ -3669,6 +3669,48 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest end end + def test_multiple_roots + draw do + namespace :foo do + root "pages#index", constraints: { host: 'www.example.com' } + root "admin/pages#index", constraints: { host: 'admin.example.com' } + end + + root "pages#index", constraints: { host: 'www.example.com' } + root "admin/pages#index", constraints: { host: 'admin.example.com' } + end + + get "http://www.example.com/foo" + assert_equal "foo/pages#index", @response.body + + get "http://admin.example.com/foo" + assert_equal "foo/admin/pages#index", @response.body + + get "http://www.example.com/" + assert_equal "pages#index", @response.body + + get "http://admin.example.com/" + assert_equal "admin/pages#index", @response.body + end + + def test_namespaced_roots + draw do + namespace :foo do + root "test#index" + end + + root "test#index" + + namespace :bar do + root "test#index" + end + end + + assert_equal "/foo", foo_root_path + assert_equal "/", root_path + assert_equal "/bar", bar_root_path + end + private def draw(&block) -- cgit v1.2.3