2

我正在寻找从他们的子域以及他们的自定义域路由用户的页面。例如,考虑三个域:

app.com
user1.app.com
user1.com

访问者应该能够在应用域 (user1.app.com) 的子域以及用户的自定义域 (user1.com) 中看到用户的页面。也就是说,访问者在访问“app.com”的任何子域或不是“app.com”的根域时将访问用户页面。

我将如何设置路线来做到这一点?

也许类似于此伪代码的内容:

match "/", :to => "user_page#show", :constraints => { :subdomain => /.+/ OR :domain => NOT(app.com) }

你怎么看?

4

1 回答 1

7

使用约束实用程序类或模块

module DomainConstraint
  def self.matches? request
    request.subdomain.present? || request.domain != 'app.com'
  end
end

constraints DomainConstraint do
  # routing here
end

如果您的约束仅适用于一条路线,您可以执行以下操作:

resources :foo, constraints: DomainConstraint

注意:您的实用程序类也可以用一个简单的lambda替换(请参阅“动态请求匹配”)

于 2013-03-27T16:04:10.887 回答