简体   繁体   中英

Separate Domain for Namespaced Routes in Rails 4

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.

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM