Rails 3 路由约束似乎没有正确匹配正则表达式
我正在使用 Rails 3.0.5,并且我已经使用正则表达式约束设置了一条路线。它曾经在 Rails 2.3.5 上工作,但在 Rails 3 中不起作用。路线如下所示:
get '/:version_id' => 'pastes#show', :constraints => { :version_id => /[\d\w]{40}/ }
它根本不起作用。然而,以下工作:
get '/:version_id' => 'pastes#show', :constraints => { :version_id => /.{40}/ }
get '/:version_id' => 'pastes#show', :constraints => { :version_id => /\w{40}/ }
get '/:version_id' => 'pastes#show'
Rails 处理 [ ] 匹配的方式是否有问题?或者我做错了什么?
version_id 通常看起来像这样:
816616001d7ce848944a9e0d71a5a22d3b546943
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有办法解释为什么一个可能无法胜过另一个。
然而,根据 PickAxe 书,
\w
实际上是\d
的超集。因此,
[\d\w]{40}
与适合您的\w{40}
没有什么不同。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
.Therefore,
[\d\w]{40}
is no different from\w{40}
, which works for you.