Domain Requirements in Routes (Edge Rails)

I saw on the bottom of the following page that you can route requests to
different controllers based upon which subdomain is being requested.

http://wiki.rubyonrails.com/rails/pages/Routes

with_options :requirements=>{:subdomain=>‘first.com’} do
map.connect ‘’, :controller => “first/catalog”
map.connect ‘featured’, :controller => “first/catalog”,
:action=>‘featured’
map.connect ‘catalog/:action/:id’, :controller => “first/catalog”
end

So you could have one home page setup for one subdomain and another home
page for another subdomain. I’ve tried implementing this with Edge
Rails, but haven’t had any luck. The :subdomain requirement doesn’t seem
to have any affect. I must be missing something. I’ve looked through the
Edge Rails documentation and haven’t seen anything that talks about
using :subdomain. Does the domain or subdomain need to be passed into
the routes somehow? Any help would be appreciated.

I just noticed what looks like an error in the code on the Wiki. I think
it should be:

map.with_options :requirements=>{:subdomain=>‘first.com’} do |m|
m.connect ‘’, :controller => “first/catalog”
m.connect ‘featured’, :controller => “first/catalog”,
:action=>‘featured’
m.connect ‘catalog/:action/:id’, :controller => “first/catalog”
end

Although this still doesn’t seem to help with the :subdomain
requirement.

I’m running Rails 1.1 RC1 and Webbrick reports a Content-Length: 0
for any dynamic content it delivers. When I serve static content
(like images) the Content-Length is calculated correctly.

I’m trying to track down an obscure bug and this is one difference
bewteen a system that is working and one that isn’t.

curl -I http://localhost:3000/page/otml/1
HTTP/1.1 200 OK
Cache-Control: no-cache
Connection: Keep-Alive
Date: Fri, 24 Mar 2006 05:45:16 GMT
Content-Type: text/xml; charset=UTF-8
Server: WEBrick/1.3.1 (Ruby/1.8.4/2005-12-24)
Content-Length: 0
Set-Cookie: _session_id=109456392dcb2d36dbd500ee5d3ddb1f; path=/

The actual Content-Length is 8620.

Has anyone seen :requirements=>{:subdomain=>whatever} in the wild? Is
it documented anywhere other than that wiki page?

On 2006-03-23 16:48:31 -0500, Ben Blakley
[email protected] said:

yeah i’ve post the problem on the railsforum.com previously but noone
answered that yet. i’m frustrated

Frustration no more. See the Request Routing plugin. With the new
routing code, it’s almost too easy!
http://agilewebdevelopment.com/plugins/request_routing

Ben Blakley wrote:

I saw on the bottom of the following page that you can route requests to
different controllers based upon which subdomain is being requested.

Peak Obsession

with_options :requirements=>{:subdomain=>‘first.com’} do
map.connect ‘’, :controller => “first/catalog”
map.connect ‘featured’, :controller => “first/catalog”,
:action=>‘featured’
map.connect ‘catalog/:action/:id’, :controller => “first/catalog”
end

So you could have one home page setup for one subdomain and another home
page for another subdomain. I’ve tried implementing this with Edge
Rails, but haven’t had any luck. The :subdomain requirement doesn’t seem
to have any affect. I must be missing something. I’ve looked through the
Edge Rails documentation and haven’t seen anything that talks about
using :subdomain. Does the domain or subdomain need to be passed into
the routes somehow? Any help would be appreciated.

i’ve installed the Request Routing plugin from that URL, but it does not
seem to work on my application. the :requirements still does not have
any effect. i can get the subdomain name with the SubdomainAsAccountKey
plugin, but the subdomain routing can’t work. :frowning:

i need subdomain routing to map this:
URL> admin.domain.com/setting # :controller=>‘admin’,
:action=>‘setting’
URL> username.domain.com/setting # :controller=>‘user’,
:action=>‘setting’

thanks in advance

Doug D. wrote:

Frustration no more. See the Request Routing plugin. With the new
routing code, it’s almost too easy!
http://agilewebdevelopment.com/plugins/request_routing

i’ve installed the Request Routing plugin from that URL, but it does not
seem to work on my application. the :requirements still does not have
any effect. i can get the subdomain name with the SubdomainAsAccountKey
plugin, but the subdomain routing can’t work. :frowning:

In the new version of Rails you need to use :conditions rather than
:requirements

Also, request routing just specifies a requirement for routing. EG.
you can say things like only use these routes if the subdomain is not
admin or whatever so it might not actually be the answer to your
problem but in this case it looks like it will work.

Cheers,


Dan W.
http://www.danwebb.net

Dan W. wrote:

i’ve installed the Request Routing plugin from that URL, but it does not
seem to work on my application. the :requirements still does not have
any effect. i can get the subdomain name with the SubdomainAsAccountKey
plugin, but the subdomain routing can’t work. :frowning:

In the new version of Rails you need to use :conditions rather than
:requirements

Also, request routing just specifies a requirement for routing. EG.
you can say things like only use these routes if the subdomain is not
admin or whatever so it might not actually be the answer to your
problem but in this case it looks like it will work.

Cheers,


Dan W.
http://www.danwebb.net

I just dealt with this use this morning. I found the info in the README
with the plugin. I forget if there are other changes.

wow it works
apperently i haven’t read the Readme file for the recent changes. i read
the old manual from somewhere on the web.
thanks Dan, you rocks :slight_smile:

it works when i tried this:

map.connect ‘setting’, :controller => ‘admin’, :action => ‘setting’,
:conditions => { :subdomain => ‘admin’ }
map.connect ‘setting’, :controller => ‘user’, :action => ‘setting’,
:conditions => { :subdomain => ‘user’ }

but it fail when previously i tried this:

map.with_options :conditions=>{:subdomain=>‘admin’} do |map|
map.connect ‘setting’, :controller => ‘admin’, :action => ‘setting’
end
map.with_options :conditions=>{:subdomain=>‘user’} do |map|
map.connect ‘setting’, :controller => ‘user’, :action => ‘setting’
end
map.with_options :conditions=>{:subdomain=>‘test’} do |map|
map.connect ‘setting’, :controller => ‘test’, :action => ‘setting’
end

the user subdomain generates ‘Recognition failed for “/setting”’. i
tried to add the test subdomain, and it generates the same error
message. did i missed something? it’s ok though, i still can the without
with_options version :slight_smile:

and somehow, i cannot connect to controller when i put an empty string
to the map connect, it still connect to the main index. any suggestion?

map.connect ‘’, :controller => ‘admin’, :action => ‘index’, :conditions
=> { :subdomain => ‘admin’ }
map.connect ‘’, :controller => ‘user’, :action => ‘index’, :conditions
=> { :subdomain => ‘user’ }

thanks,

Adrian L.

Dan W. wrote:

In the new version of Rails you need to use :conditions rather than
:requirements

Also, request routing just specifies a requirement for routing. EG.
you can say things like only use these routes if the subdomain is not
admin or whatever so it might not actually be the answer to your
problem but in this case it looks like it will work.

Cheers,


Dan W.
http://www.danwebb.net