Question

I have defined a few types of users using devise (members, company_users, etc) and I'd like to use different subdomains for the login pages of each type of user.

I've referred to this railscast in order to implement the matching of the subdomain and redirect to the appropriate action. My routes.rb file looks like this:

devise_for :company_users, :controllers => { :registrations => 'company_users/registrations', :sessions => 'company_users/sessions'  }

devise_scope :company_user do
  constraints Subdomain do
    match '/' => 'company_users/sessions#new'
  end
end

And my lib/subdomain.rb file:

class Subdomain
  def self.matches?(request)
    request.subdomain.present? and request.subdomain =~ /\Acompanies\z/
  end
end

Locally, it works perfectly. I've tested using companies.lvh.me:3000 (as the same railscast suggests) and it really redirects to the correct login page.

In order to try and make it work on Heroku I have added the domain, using heroku domains:add companies.mydomain.com, and I have added a new CNAME record on my DNS server, pointing to my Heroku application.

However, when I try to access companies.mydomain.com it redirects me to the root path, and not to the correct login page. I'm kind of clueless of what's happening. Any help will be appreciated.

Was it helpful?

Solution

This happen when the tld of your domain is different from tld of the heroku domain.

Mine is .com.br and I have to add config.action_dispatch.tld_length = 2 to production.rb, so Rails can parse the URL correctly and redirect to the right subdomain.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top