Rails 带约束的路由 Class 未按预期工作

Rails Routing with Constraint Class not working like expected

我有一个更复杂的路由约束,为什么这个简单的例子不起作用:

class FooBar
  def self.matches?(request)
    true
  end
end

get ':foo', to: redirect('/bar'), constraints: FooBar.new

我得到的只是

Invalid constraint: #<FooBar:0x007f87f14dce40> must respond to :call or :matches?

有什么想法可以让它发挥作用吗?谢谢

must respond to :call or :matches?

什么意思 FooBar 的实例必须有一个方法(不是你代码中的 class 方法)matches:

class FooBar
  def matches?(request)
    true
  end
end

或者在我的示例中回复callproc

FooBar = proc do |request|
   # here goes code
end

get ':foo', to: redirect('/bar'), constraints: FooBar