简体   繁体   English

不能在application.rb中要求文件

[英]Cannot require file in application.rb

I'm trying to simplify our configuration by creating small configuration classes that can be included in our application.rb. 我试图通过创建可以包含在application.rb中的小型配置类来简化我们的配置。

lib/logging.rb lib / logging.rb

class << Logging

    def configure(config)
        # ... configure logging stuff
    end

end

application.rb application.rb

require 'lib'
module MyApp
  class Application < Rails::Application

      Logging.configure(config)

  end
end

The problem is if I don't use require "lib" then I get an Undefined Constant Logging error. 问题是,如果我不使用require "lib"则会收到Undefined Constant Logging错误。 But if I try to require it I get: 但是,如果我尝试要求它,则会得到:

bin/rails:6: warning: already initialized constant APP_PATH
/opt/qtip/bin/rails:6: warning: previous definition of APP_PATH was here

The only way I've been able to get it to work is by doing this which is very limiting. 我能够使其正常工作的唯一方法是这样做,这是非常有限的。

config.autoload_paths = %w(lib)

config.after_initialize do
  ::Logging.configure(config)
end

You have wrong class declaration. 您的类声明有误。

Instead 代替

class << Logging

you should use 你应该使用

class Logging
  class << self
    def configure(config)
    end
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM