newUserNameHere
newUserNameHere

Reputation: 18001

Rails route constraint on subdomain not matching regex route

I have a route that looks like this:

match '/', to: 'browse#city_companies', constraints: { subdomain: /[a-z\-]+-[a-z]{2}/ }, :via => [:get]

The regex /[a-z\-]+-[a-z]{2}/ used in the constraint will match for example: "birmingham-al", but not "south-carolina". Which would be the behavior I'm trying to achieve.

However, rails is using this route on both "birmingham-al" and "south-carolina" which is not what I want. What am I doing wrong here?

Upvotes: 1

Views: 988

Answers (1)

yoavmatchulsky
yoavmatchulsky

Reputation: 2960

You forgot to use the end of string character:

/[a-z\-]+-[a-z]{2}\z/

or

/[a-z\-]+-[a-z]{2}$/

and the route can be:

get '/', to: 'browse#city_companies', constraints: { subdomain: /[a-z\-]+-[a-z]{2}\z/ }

Upvotes: 1

Related Questions