Routing for multiple subdomains
I'm trying to specify routes (specifically, the root
route), dependent on what the current subdomain. I want to route to controller#action1
if the subdomain is subdomain
; to controller#action2
if the subdomain is present but not subdomain
or www
; and to controller#action3
if the subdomain is www
or not given.
I tried the following:
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
Browsing to subdomain.myapp.com
and other.myapp.com
both work as expected (going to action1
and action2
, respectively). However, when I try myapp.com
or www.myapp.com
, I get:
Routing Error
No route matches [GET] "/"
When I run rake routes
, I see
开发者_JAVA百科 / {:subdomain=>"subdomain", :controller=>"controller", :action=>"action1"}
/ {:controller=>"controller", :action=>"action2"}
root / {:controller=>"controller", :action=>"action3"}
So I guess (speculating) that because there are two routes for /
that don't specify a subdomain, my fallback route tries to match the second but then doesn't meet the constraints and fails? What can I do to get this working properly?
This seems to have been a bug in the routing-filter gem, and seems to be fixed as of more recent versions of the gem and the routing changes in Rails 3.2.
精彩评论