Reputation: 176
So I have a route with a constraint that matches paths such as the following:
/jobs-in-London
get ':id' => 'search#show', :constraints => { :id => /jobs-in-[A-Z].*/ }
This works great, but I need to match to locations written in other character sets, such as Japanese. I am happy for the "jobs-in-" to remain in english, since the valid urls are generated from a list of locations uploaded by the user.
This means I would be expecting to match /jobs-in-東京
, which would then search in Tokyo once it hit the controller. Geolocation is done later, and works without translation. I just need to get the request to the right controller.
I have tried this:
get ':id' => 'search#show', :constraints => { :id => /jobs-in-\p{L}*/ }
which I constructed using Rubular, and it appears to match correctly as seen here. I am aware this doesn't have the capital letter constraint, but that doesn't matter as some languages don't seem to have true capital letters anyway.
However the routes don't match it, and instead spit it out to the wildcard catch all at the bottom.
Am I falling foul of how Rails implements regex for routes? Does it not support the unicode selectors?
I cannot have this:
get ':id' => 'search#show', :constraints => { :id => /jobs-in-.*/ }
as this matches /jobs-in-$£@
which is undesired, I want to constrain it to "letter" characters.
Upvotes: 1
Views: 614
Reputation: 16294
I wanted to match routes like /foo/a
and /foo/ä
, where that last bit could be non-ASCII letters like "ä".
On Rails 4.2, I had no luck with
get "foo/:letter" => "foos#bar", letter: %r{[[:alpha:]]}
or other variations of the regex that I tried, but this worked fine:
get "foo/:letter" => "foos#bar",
constraints: ->(request) {
# Matching with `letter: some_regex` did not work with åäö.
letter = request.path_parameters[:letter]
letter.match(/\A[[:alpha:]]\z/u)
}
Upvotes: 1