Defining Subdomains for Local Development
When you begin developing an application using subdomains, you need to move beyond using http://locahost:3000 as we need to be able to develop and test with subdomains. You can open up your /etc/hosts (within a Unix-based O/S) file and add the following.
127.0.0.1 localhost
127.0.0.1 lifestyle.dev
127.0.0.1 life2go.lifestyle.dev
127.0.0.1 vitalzeu.lifestyle.dev
This will let you make request to http://lifestyle.dev:3000 and http://life2go.lifestyle.dev:3000
It's important to remember that the subdomain must be specified here to work for local requests.
Unfortunately hosts file dont support wild cards(*).
Detecting Subdomains in Rails
In the application.rb file add this line to find subdomains
@subdomains = request.subdomains.first
The subdomains method isn't particularly smart, all it does is splitting on.and returning all but the
last two parts. So to get it to work in development then you will need to have the same number
of parts to your domain as you will in production.
So if you'd have life2go.lifestyle.com in production then you will need to have three part domain
names in development too, so something like life2go.lifestyle.dev mapped to your 127.0.0.1 address.
Take a look at the doco for more information.
The request.subdomains.first will work both in production and development environments.
Advertisement