Separate Domain for Namespaced Routes in Rails 4

后端 未结 1 513
天涯浪人
天涯浪人 2021-01-15 00:33

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

相关标签:
1条回答
  • 2021-01-15 01:16

    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')
    
    0 讨论(0)
提交回复
热议问题