先上代码,这是我的 routes.rb 设定:
namespace :admin, path: "/", constraints: { subdomain: 'admin' } do
get "/" => "dashboard#index"
end
root to: "pages#index"
get "about" => "pages#about"
结果如下:
pages#index
pages#about
admin/dashboard#index
问题来了,当访问 admin.sample.dev/about 还是会得到 pages#about
!
我以为 namespace + constraints subdomain 会形成一个「闭包」,我要怎么防止 admin.sample.dev/about 存取到 pages#about
?
目前只想到在 namespace 里面覆写一个 get "about" 设定但这样写好丑……来请教有没有其他作法?
把 root 层的设定包到一个 constraints 就解决了:
namespace :admin, path: "/", constraints: { subdomain: 'admin' } do
get "/" => "dashboard#index"
end
constraints Rootdomain do
root to: "pages#index"
get "about" => "pages#about"
end