我正在按照导轨指南进行高级约束Advanced Constraints。下面是代码:我应该在哪里为routes.rb定义一个约束?
class BlacklistConstraint
def initialize
@ips = Blacklist.retrieve_ips
end
def matches?(request)
@ips.include?(request.remote_ip)
end
end
Rails.application.routes.draw do
get '*path', to: 'blacklist#index',
constraints: BlacklistConstraint.new
end
的导游没有提到其中BlacklistConstraint
应该被定义或者是否它遵循命名约定。我试图按照这个例子为自己使用,但我不断得到UninitialiezedConstantError: Can someone help me out? So far I;ve defined my constraint class in the 1
routes.rb file itself and in the
lib`目录。两种方法都不起作用。
感谢您的帮助,但由于某种原因,这并不适合我。我不得不在我的'application.rb'中需要文件路径。 –
您必须打开自动加载/预先加载'lib'文件夹中的文件。 http://blog.bigbinary.com/2016/08/29/rails-5-disables-autoloading-after-booting-the-app-in-production.html – Iceman