Rails 3 路由约束似乎没有正确匹配正则表达式

发布于 2024-10-22 19:07:02 字数 623 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纸短情长 2024-10-29 19:07:02

我没有办法解释为什么一个可能无法胜过另一个。

然而,根据 PickAxe 书,\w 实际上是 \d 的超集。

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

因此,[\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.

\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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文