user681768
user681768

Reputation:

Is there a way to specify a default subdomain in Rail's named routes?

I'm working with subdomains in my Rails 3.2 app where request.subdomain is meaningful and is used to populate an Account resource.

A default behavior I expected of named routes was that regardless of what subdomain I was at, a named route would always use 'www' before hostname unless I used the subdomain option.

Example, if I were at http://www.example.com/path/to/something, the following is true:

root_url                          # http://www.example.com/
root_url(subdomain: 'subdomain')  # http://subdomain.example.com/ 

Even though it makes sense what I didn't exact was that if I were at http://subdomain.example.com/path/to/something, the following would be the case:

root_url                          # http://subdomain.example.com/

This is fine, but a lot of my controller redirects and view helper links are for urls outside of my subdomain constraint which omits 'www' and ''. So I really don't want to have new_session_url(subdomain: 'www'), thing_url(subdomain: 'www') and etc. all over my application.

Is there a way to have named routes default to 'www' if no subdomain option is passed in, regardless of what the current subdomain is?

Upvotes: 1

Views: 1016

Answers (1)

Waley Chen
Waley Chen

Reputation: 929

Wrap the routes with the following:

defaults :subdomain => '' do
     routes
end

Upvotes: 3

Related Questions