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

发布于 2024-11-30 08:13:59 字数 1373 浏览 2 评论 0原文

为什么会出现错误?

这是设置:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤独难免 2024-12-07 08:13:59

您在任何地方都不需要此文件。这就是为什么它找不到常数。 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文