3
我試圖指定路由(具體地說,root
路由),取決於當前的子域。如果子域爲subdomain
,我想路由到controller#action1
;到controller#action2
如果子域存在但是不是subdomain
或www
;如果子域是www
或者沒有給出,則返回controller#action3
。多個子域的路由
我試過如下:
MyApp::Application.routes.draw do
scope :constraints => { :subdomain => "subdomain" } do
match "/" => "controller#action1"
end
scope :constraints => lambda {|req| req.subdomain.present? && !%w(subdomain www).include?(req.subdomain) } do
match "/" => "controller#action2"
end
root :to => "controller#action3"
end
瀏覽到subdomain.myapp.com
和other.myapp.com
預期都工作(去action1
和action2
,分別)。然而,當我嘗試myapp.com
或www.myapp.com
,我得到:
Routing Error
No route matches [GET] "/"
當我運行rake routes
,我看到
/{:subdomain=>"subdomain", :controller=>"controller", :action=>"action1"}
/{:controller=>"controller", :action=>"action2"}
root/{:controller=>"controller", :action=>"action3"}
,所以我猜(猜測),因爲有兩條路爲/
,唐」 t指定一個子域名,我的回退路由嘗試匹配第二個,但是不符合約束條件並失敗?我能做些什麼才能正常工作?