如果rails中存在子域,则单独的路由

时间:2011-09-17 17:23:41

标签: routing ruby-on-rails-3.1

当子域存在时,有没有办法将rails 3.1中的路由分开?我想在使用子域时使用一个路由集合,如果不使用则使用一个。

例如

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

这样的事情可能吗?

1 个答案:

答案 0 :(得分:3)

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