Home > Software engineering >  Rails routes exclude domain from constraints host
Rails routes exclude domain from constraints host

Time:02-02

In my Rails 6 app I have a few subdomains using a set o routes. To make those routes only available to certain subdomains I use:

constraints(:host => 'app.example.com') do
  scope :module => :ui do
    # Some resources and additional routes here
  end
end

Is there a way to make a set of routes available to all subdomains except this one

constraints(:host IS NOT 'app.example.com') do
  scope :module => :ui do
    # Some resources and additional routes here
  end
end

This IS NOT is phantom code so make clear what I would like to achieve. The routes should only be available if the subdomain IS NOT app.example.com

CodePudding user response:

We use something like this to make host specific constraint. Let me know if this works for you.

scope constraints: lambda { |req| req.host != 'app.example.com' } do
  # Some resources and additional routes here
end

CodePudding user response:

We can use advanced constraint as well

class RestrictedConstraint
  def self.matches?(request)
    !['localhost', 'app.example.com'].include?(request.host)
  end
end

Rails.application.routes.draw do

  scope constraints: RestrictedConstraint do
    # Some resources and additional routes here
  end

end
  •  Tags:  
  • Related