带有约束的 Rails 自定义路由 - 路由要求中不允许使用正则表达式锚字符 [英] Rails custom route with constraints - regexp anchor characters are not allowed in routing requirements

查看:73
本文介绍了带有约束的 Rails 自定义路由 - 路由要求中不允许使用正则表达式锚字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下路线:

  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_]+$/

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

So I get that the ^ character isn't allowed, but not sure what character is producing this particular routing error.

推荐答案

在正则表达式中,我们有两个锚点:

In regex we have two anchors:

  1. 行/字符串开头^
  2. 行/字符串结束 $

<小时>

尝试从模式中删除 $ ,您应该很高兴...


Try to remove $ from the pattern and you should be good to go...

这篇关于带有约束的 Rails 自定义路由 - 路由要求中不允许使用正则表达式锚字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆