I am using Rails and I want to use contraint in route to exclude that route if keyword "incident" is anywhere in the url.
I am using rails3.
Here is my existing routes.
match ':arg', :to => "devices#show", :constraints => {:arg => /???/}
I need to put something in constraints so that it does not match if word "incident" is there.
Thanks
Instead of bending regular expressions a way it is not intended to, I suggest this approach instead:
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
...
Its a lot more verbose, but in the end more elegant I think.
Adding onto @Johannes answer for rails 4.2.5:
config/routes.rb (at the VERY end)
constraints(RouteConstraint) do
get "*anythingelse", to: "rewrites#page_rewrite_lookup"
end
config/initializers/route_constraint.rb
class RouteConstraint
def self.matches?(request)
not ["???", "Other", "Engine", "routes"].any? do |check|
request.env["REQUEST_PATH"].include?(check)
end
end
end
(?!.*?incident).*
might be what you want.
This is basically the same question as How to negate specific word in regex?. Go there for a more detailed answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With