const_missing':未初始化的常量Rack :: IpRestrictor(NameError)

时间:2011-08-18 21:42:38

标签: ruby-on-rails ruby-on-rails-3 rack middleware

为什么会出错?

以下是设置:

配置/初始化/ 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

配置/ 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?

由于

1 个答案:

答案 0 :(得分:0)

您不需要在任何地方使用此文件。这就是为什么它找不到常数。 lib目录中的文件不会自动加载到Rails 3中。手动需要此文件。