Question

I am using Rails 3.0.5 and I have setup a route using a regex constraint. It used to work on Rails 2.3.5, but it's not working in Rails 3. The route looks like this:

get '/:version_id' => 'pastes#show', :constraints => { :version_id => /[\d\w]{40}/ }

It doesn't work at all. However, the following work:

get '/:version_id' => 'pastes#show', :constraints => { :version_id => /.{40}/ }

get '/:version_id' => 'pastes#show', :constraints => { :version_id => /\w{40}/ }

get '/:version_id' => 'pastes#show'

Is there something wrong with the way Rails handles [ ] matching? or am I doing something wrong?

version_id usually looks something like this:

816616001d7ce848944a9e0d71a5a22d3b546943
Was it helpful?

Solution

I don't have a solution as to why one may not work over the other.

However, according to the PickAxe book, \w is actually a superset of \d.

\w  [A-Za-z0-9\_]   ASCII word character
\d        [0-9]     ASCII decimal digit character

Therefore, [\d\w]{40} is no different from \w{40}, which works for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top