Rails: request.subdomain not populating if host is localhost

Created on 4 Oct 2013  路  3Comments  路  Source: rails/rails

I have been trying to route to different controllers for different subdomains, without any luck. This is what I had

Petworkslabs::Application.routes.draw do
  get '/', to: 'custom#show', constraints: {subdomain: '/.+/'}, as: 'custom_root'
  get '/',  to: "welcome#home", as: 'default_root'
end

But for some reason the request getting routed to welcome#home. I stepped through the debugger and found that request.subdomain was being populated as nil, even though request.domain was populated as 'abc.localhost'. I suspect this subdomain is not getting populated because I am doing this on localhost, but it will be nice to have this work on localhost for development purposes. I ended by creating a custom matches? for the Subdomain class which looks something like this

class Subdomain
  def self.matches?(request)
    request.domain.split('.').size>1 && request.subdomain != "www"
  end
end
routing

Most helpful comment

If you look a the documentation for subdomain you can see that it takes an optional argument tld_length which defaults to 1. This means that when calculating the domain and subdomain it will assume that first component on the right is a top-level domain. If you pass 0 for tld_length then it will work as you want. Similarly, if you pass 2 it'll work for country domains like pixeltrix.co.uk, e.g:

# tld_length = 1 (Default)
>> ActionDispatch::Http::URL.extract_subdomains('abc.localhost').first
=> nil
# tld_length = 0
>> ActionDispatch::Http::URL.extract_subdomains('abc.localhost', 0).first
=> "abc"
# tld_length = 2
>> ActionDispatch::Http::URL.extract_subdomains('www.pixeltrix.co.uk', 2).first
=> "www"

You can configure the default tld_length in your config/environments/development.rb file, e.g:

MyApp::Application.configure do
  config.action_dispatch.tld_length = 0
end

Obviously, you can also configure it for test and production environments as well.

All 3 comments

Out of curiousity, are you using Pow as your server or rails server?

If you look a the documentation for subdomain you can see that it takes an optional argument tld_length which defaults to 1. This means that when calculating the domain and subdomain it will assume that first component on the right is a top-level domain. If you pass 0 for tld_length then it will work as you want. Similarly, if you pass 2 it'll work for country domains like pixeltrix.co.uk, e.g:

# tld_length = 1 (Default)
>> ActionDispatch::Http::URL.extract_subdomains('abc.localhost').first
=> nil
# tld_length = 0
>> ActionDispatch::Http::URL.extract_subdomains('abc.localhost', 0).first
=> "abc"
# tld_length = 2
>> ActionDispatch::Http::URL.extract_subdomains('www.pixeltrix.co.uk', 2).first
=> "www"

You can configure the default tld_length in your config/environments/development.rb file, e.g:

MyApp::Application.configure do
  config.action_dispatch.tld_length = 0
end

Obviously, you can also configure it for test and production environments as well.

@pixeltrix Thanks for the clarification!

Was this page helpful?
0 / 5 - 0 ratings