Obromios
Obromios

Reputation: 16373

How to do wild card subdomains in rails 5

In Ryan Bates railscast on subdomains, he gives the following way to match subdomains

# config/routes.rb
match '', to: 'pro_users#show', contraints: lambda { |r| r.subdomain.present? && r.subdomain != 'www' }

but match without the HTTP verb was deprecated in rails 5, so this gives an exception

You should not use the `match` method in your router without specifying an HTTP method.

So how do you do this in rails 5 and above?

Upvotes: 0

Views: 246

Answers (1)

Mohit Kumar
Mohit Kumar

Reputation: 53

In rails 5, you have to specify which HTTP verb that you want to use. So in if you want to use that route for get and post, you would write

# config/routes.rb
    match '', to: 'pro_users#show', via: [:get,:post], contraints: lambda { |r| r.subdomain.present? && r.subdomain != 'www' }

You might also like to clean up the constraint by using a contraint class, this blog has some suggestions.

So you could write the route matcher as

# config/routes.rb
match '', to: 'pro_users#show', via: [:get,:post], contraints: SubdomainConstraint }

and

class SubdomainConstraint
   def self.matches?(request)
     request.subdomain.present? && request.subdomain != 'www'
  end
end

Upvotes: 1

Related Questions