Mysterywood
Mysterywood

Reputation: 1668

Ruby on Rails - Excluding a single domain from some routes

In Rails route, usually we do constraints: {domain: "example.com" } if we want to specify specific routes that example.com can have. But how do I reverse this such that everyone can access this except example.com

Upvotes: 1

Views: 82

Answers (1)

demir
demir

Reputation: 4709

You can create custom constraint. Add more excluded domains if you want.

class DomainConstraint
  def matches?(request)
    excluded_hosts = ['example.com']
    excluded_hosts.exclude?(request.host)
  end
end

Use it like this:

constraints DomainConstraint.new do
  ..
end

https://guides.rubyonrails.org/routing.html#advanced-constraints

Upvotes: 2

Related Questions