7

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"

1 Answer 1

10

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Is there some way to list as a constraint that the param 'email' must exist? As in /url.com?email=emailaddress, and to route one way if email exists and another if it doesn't?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.