简体   繁体   中英

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.

lib/logging.rb

class << Logging

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

end

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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