【问题标题】:Ruby on Rails: Routing for subdomain and custom domain possible?Ruby on Rails:子域和自定义域的路由可能吗?
【发布时间】:2013-03-17 17:14:35
【问题描述】:

我希望从用户的子域和自定义域路由用户的页面。例如,考虑三个域:

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

你怎么看?

【问题讨论】:

  • 我认为这不是路由问题。只需在您的 Rails 应用程序 request.domainrequest.subdomain 中检查请求

标签: ruby-on-rails ruby routes


【解决方案1】:

使用a constraint utility class or module

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(请参阅“动态请求匹配”)

【讨论】:

  • 接受匿名用户的编辑建议。我不知道为什么审稿人拒绝了他们(将正则表达式 =~ /.+/ 替换为 present?
猜你喜欢
  • 1970-01-01
  • 2012-04-03
  • 2012-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-28
  • 2011-04-09
相关资源
最近更新 更多