Matt Walston
Matt Walston

Reputation: 119

Do not match routes outside of subdomain constraint for requests in that subdomain

From routes.rb:

constraints subdomain: 'admin' do
  scope module: 'admin', as: 'admin' do
    resources :subscribers
    root 'dashboard#index'
  end
end

resources :users
root 'dashboard#index'

Under current snippet GET admin.domain.xzy/users still triggers users controller action. I understand that the rules will continue to be parsed until one specifies. Is there a way to modify this behavior? Such that for the subdomain constraint, the router will only search within that block.

Upvotes: 2

Views: 465

Answers (1)

Mohamad
Mohamad

Reputation: 35349

Why not put the offending line inside another constraint?

  constraints(NoSubdomain) do
    resources :users
  end

The constraint would look something like this:

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

Upvotes: 3

Related Questions