sandelius
sandelius

Reputation: 527

Separate routes if subdomain is present in rails

Is there a way to separate routes in rails 3.1 when a subdomain is present? I want to use one collection of routes when subdomains is used and one if not.

e.g

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

Is something like this possible?

Upvotes: 4

Views: 737

Answers (1)

99miles
99miles

Reputation: 11222

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

Upvotes: 4

Related Questions