当存在子域时,有没有办法在 rails 3.1 中分离路由?我想在使用子域时使用一组路由,如果不使用则使用一组。

例如

if request.subdomain.present?
  root ....
  resources ...
else
  root ....
  resources ...
end

这样的事情可能吗?

最佳答案

class SubdomainRoute

  def self.matches?(request)
    request.subdomain.present? && request.subdomain != "www"
  end

end
class NoSubdomainRoute

  def self.matches?(request)
    !request.subdomain.present?
  end

end
  constraints(NoSubdomainRoute) do
    resources :profile # matches if there is not a subdomain
  end

  constraints(SubdomainRoute) do
    resources :profile # matches if there is a subdomain
  end

关于routing - 如果子域存在于 rails 中,则单独路由,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7456535/

10-12 01:06