代码之家  ›  专栏  ›  技术社区  ›  Chowlett

带有扩展regex的rails 3路由:约束-bug?

  •  1
  • Chowlett  · 技术社区  · 14 年前

    我的Rails 3应用程序中有以下路径。

    post 'games/:id/:task/:card_id' => 'games#perform', :as => :perform
    

    …这显然允许 button_to("Foo", {card_id=>2, :action=>:perform, :task=>"foo"}) ,映射到URL /games/1/foo/2 .

    我想限制路由匹配的任务集。这个 Rails API docs 指示“约束可以包括ignorecase和扩展语法正则表达式修饰符”。但是,以下工作符合预期:

    post 'games/:id/:task/:card_id' => 'games#perform', :as => :perform, :constraints => {:task => /(foo|bar)/}
    

    但以下情况并非如此:

    post 'games/:id/:task/:card_id' => 'games#perform', :as => :perform, :constraints => {:task => /(foo|
                                                                                                     bar)/x}
    

    在后一种情况下, button_to 上面的链接生成URL: /games/perform?card_id=2&task=foo .

    这是一个错误,还是我做错了什么?

    1 回复  |  直到 14 年前
        1
  •  1
  •   smathy    13 年前

    是的,里面有个虫子 RegexpWithNamedGroups 处理Ruby1.8.x,因为它以与regexp修饰符不兼容的方式伪造了命名的regexp groups语法1.9。

    这里的问题是,对于1.8 Rails,使用regexp中paren组的简单索引映射到名称。当您向regexp添加修饰符时,这会在内部创建一个额外的组,该组将索引抛出一个。

    现在的解决方法是使用1.9,或者不使用修饰符。