2

I'm working on a Rails 4 app. One part of the app is a customer portal that has to be accessed from a separate domain.

I have everything working fine by navigating to domain.com/cp. The routes use namespaced controllers:

namespace :cp do
    get :dashboard, to: 'dashboard#index', path: ''
    ...
end

How should I set up DNS records and change the routes definition so that another domain cpdomain.com points to domain.com/cp properly (no redirecting).

Thanks.

Martin Sojka
  • 523
  • 5
  • 18

1 Answers1

4

This answer can be useful for the rails routes problem:

Rails routing to handle multiple domains on single application

Shortened:

1) define a custom constraint class in lib/domain_constraint.rb:

class DomainConstraint
  def initialize(domain)
    @domains = [domain].flatten
  end

  def matches?(request)
    @domains.include? request.domain
  end
end

2) use the class in your routes with the new block syntax

constraints DomainConstraint.new('mydomain.com') do
  root :to => 'mydomain#index'
end

root :to => 'main#index'

or the old-fashioned option syntax

root :to => 'mydomain#index', :constraints => DomainConstraint.new('mydomain.com')
Community
  • 1
  • 1
Jorge de los Santos
  • 4,583
  • 1
  • 17
  • 35