Rails 4 中命名空间路由的单独域 [英] Separate Domain for Namespaced Routes in Rails 4
问题描述
我正在开发一个 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.
导航到 domain.com/cp
,一切正常.路由使用命名空间控制器:
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
我应该如何设置 DNS 记录并更改路由定义,以便另一个域 cpdomain.com
正确指向 domain.com/cp
(无重定向).
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).
谢谢.
推荐答案
这个答案对 Rails 路由问题很有用:
This answer can be useful for the rails routes problem:
缩短:
1) 在 lib/domain_constraint.rb 中定义自定义约束类:
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) 使用新的块语法在路由中使用该类
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'
或老式的选项语法
root :to => 'mydomain#index', :constraints => DomainConstraint.new('mydomain.com')
这篇关于Rails 4 中命名空间路由的单独域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!