const_missing':未初始化的常量 Rack::IpRestrictor (NameError)
为什么会出现错误?
这是设置:
config/initializers/rack_ip_restrictor.rb
Rack::IpRestrictor.configure do
respond_with [403, {'Content-Type' => 'text/html'}, '']
ips_for :test do
add '127.0.0.1'
add '127.0.0.2/8'
end
restrict /^\/admin/, '/admin', :only => :test
end
config/application.rb
class Application < Rails::Application
...
config.middleware.use Rack::IpRestrictor.middleware
...
end
/lib/rack_ip_restrictor.rb
require 'ipaddr'
require 'active_support/core_ext/array/extract_options'
# namespace Rack
module Rack
# namespace IpRestrictor
module IpRestrictor
class << self
attr_reader :config
# @see Config#initialize
def configure(&block)
@config = IpRestrictor::Config.new
@config.instance_eval &block
end
# Rack middleware
# @return [Middleware] The configured plug & play Rack middleware
def middleware
IpRestrictor::Middleware
end
end
end
end
require 'rack_ip_restrictor/ip_group'
require 'rack_ip_restrictor/middleware'
require 'rack_ip_restrictor/config'
require 'rack_ip_restrictor/restriction'
知道为什么 Rails 不能找到 Rack::IpRestrictor 吗?
谢谢
Why the error?
Here's the setup:
config/initializers/rack_ip_restrictor.rb
Rack::IpRestrictor.configure do
respond_with [403, {'Content-Type' => 'text/html'}, '']
ips_for :test do
add '127.0.0.1'
add '127.0.0.2/8'
end
restrict /^\/admin/, '/admin', :only => :test
end
config/application.rb
class Application < Rails::Application
...
config.middleware.use Rack::IpRestrictor.middleware
...
end
/lib/rack_ip_restrictor.rb
require 'ipaddr'
require 'active_support/core_ext/array/extract_options'
# namespace Rack
module Rack
# namespace IpRestrictor
module IpRestrictor
class << self
attr_reader :config
# @see Config#initialize
def configure(&block)
@config = IpRestrictor::Config.new
@config.instance_eval &block
end
# Rack middleware
# @return [Middleware] The configured plug & play Rack middleware
def middleware
IpRestrictor::Middleware
end
end
end
end
require 'rack_ip_restrictor/ip_group'
require 'rack_ip_restrictor/middleware'
require 'rack_ip_restrictor/config'
require 'rack_ip_restrictor/restriction'
Any idea why rails can't find Rack::IpRestrictor ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在任何地方都不需要此文件。这就是为什么它找不到常数。 Rails 3 中不会自动加载
lib
目录中的文件。需要手动加载此文件。You're not requiring this file anywhere. That is why it cannot find the constant. Files in the
lib
directory are not automatically loaded in Rails 3. Require this file manually.