gpt4 book ai didi

regex - 带有约束的 Rails 自定义路由 - 路由要求中不允许使用正则表达式 anchor 字符

转载 作者:行者123 更新时间:2023-12-03 23:14:30 24 4
gpt4 key购买 nike

我有以下路线:

  get 'users/:user_id/:name', to: 'profiles#show',
:constraints => { :name => /[a-zA-Z0-9_]+$/ }, as: 'user_profile'

产生错误:
Regexp anchor characters are not allowed in routing requirements: /[a-zA-Z0-9_]+$/

所以我知道不允许使用 ^ 字符,但不确定是什么字符产生了这个特定的路由错误。

最佳答案

正则表达式 anchor 是 ^$ ,但他们在这里没有取得任何成就。 "(Y)ou don’t need to use anchors because all routes are anchored at the start." .

所以约束:

:constraints => { :name => /[a-zA-Z0-9_]+/ }

会做你想做的 - 确保名称由 1 个或多个这些字符组成,没有别的。顺便说一句,您可以简化正则表达式:
:constraints => { :name => /\w+/ }

关于regex - 带有约束的 Rails 自定义路由 - 路由要求中不允许使用正则表达式 anchor 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13222382/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com