Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 - Conditional Routing?

I have a routing question about rails 3, and setting up a conditional :root path.

Right now, my route.rb has the following:

root :to => "topics#index"

This is great and dandy, but only if a user is on their specific subdomain (basecamp style) on my site. If they go to www.myapp.com or myapp.com, this should not be the same :root. I was wonder if this was at all possible to setup, something that would be like...

if default_subdomain(www, "")
root :to => "promos#index"
else
root :to => "topics#index
end

I know this wouldn't be allowed in the routes.rb, but something that would do the same logical thing. Does anyone have any experience with this, or any documentation/blog that I could read over to try to set something like this up.

Thanks

Per chuck's help below(thanks a ton), this ended up being my working code:

constraints(:subdomain => "www") do
  root :to => "promos#index"
  end

  root :to => "topics#index"
like image 963
Kombo Avatar asked Mar 15 '11 20:03

Kombo


1 Answers

You can use the :requirements tag to accomplish this.

root :to => "promos#index", :requirements => { :subdomain => "www" }
root :to => "topics#index"

I think this will work. I've never encountered it going by sub-domain/lack of a subdomain.

Edit: After doing some reading, Rails 3 uses :constraints instead.

like image 187
Chuck Callebs Avatar answered Sep 20 '22 20:09

Chuck Callebs