【问题标题】:Ruby on Rails exclude group elements in route constraintsRuby on Rails 在路由约束中排除组元素
【发布时间】:2013-04-01 09:10:23
【问题描述】:

我想知道,如何为我的路线定义约束正则表达式,只有当它排除组中的每个元素时才会匹配。

例如,我想这样做:

get "list/:action", :constraints => {:action => [NONE OF THE FOLLOWING: (new, edit, delete, update)}

我知道如果我们希望它匹配列表中的任何元素,我们会这样定义:

get "list/:action", :constraints => {:action => /(new|edit|delete|update)/}, 但我不知道,如何使它按上述方式工作。

我曾尝试在小组前使用^! 标志,但到目前为止还没有运气——我认为,该标志必须有另一种表示法。

如何排除这些元素?

【问题讨论】:

    标签: ruby-on-rails regex ruby-on-rails-3 routes constraints


    【解决方案1】:

    试试下面的。它将从路由中排除操作。

    class ExcludeActions
      def matches?
        ["new", "edit", "delete", "update"].exclude? params[:action]
      end
    end
    
    get "list/:id", :constraints => ExcludeActions.new
    

    这将从list 的路由中排除neweditdeleteupdate 操作

    【讨论】:

    • 不,我不能使用资源指令。我只需要使用约束。
    • 该类应该写在一个名为exclude_actions.rb的单独模型中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-17
    • 1970-01-01
    • 1970-01-01
    • 2015-05-15
    • 2014-11-08
    相关资源
    最近更新 更多