添加约束以路由以排除某些关键字

Nic*_*ilt 8 ruby-on-rails ruby-on-rails-3

我正在使用Rails,我想在路由中使用禁令来排除该路由,如果关键字"incident"在url中的任何位置.

我正在使用rails3.

这是我现有的路线.

match ':arg', :to => "devices#show", :constraints => {:arg => /???/} 
Run Code Online (Sandbox Code Playgroud)

我需要在约束中放置一些内容,以便在出现"事件"字时它不匹配.

谢谢

Joh*_*nes 7

而不是以一种不打算的方式弯曲正则表达式,我建议使用这种方法:

class RouteConstraint
  def matches?(request)
    not request.params[:arg].include?('incident')
  end
end

Foo::Application.routes.draw do
  match ':arg', :to => "devices#show", :constraints => RouteConstraint.new
  ...
Run Code Online (Sandbox Code Playgroud)

它更冗长,但最终我觉得更优雅.


Chr*_*nig 6

(?!.*?incident).*
Run Code Online (Sandbox Code Playgroud)

可能是你想要的.

这与如何否定正则表达式中的特定单词基本相同.去那里寻求更详细的答案.