简体   繁体   中英

How do I load a module/class from my config/application.rb file in Rails 4?

I'm using Rails 4.2.3. I've created a file at lib/app_config/aws_secrets.rb, which looks like

require 'yaml'

module mycoAppConfig
  class AwsSecrets

    def self.load
      …
    end
  end
end

Then in my config/application.rb file, I have invoked the above method using

AppConfig::AwsSecrets.load

But when I run a rake task, I'm getting this uninitialized constant error

NameError: uninitialized constant MycoWeb::Application::AppConfig
/Users/myuser/Documents/workspace/myco/myapp/config/application.rb:120:in `block in <class:Application>'
/Users/myuser/.rvm/gems/ruby-2.4.5@myapp/gems/activesupport-4.2.10/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
/Users/myuser/.rvm/gems/ruby-2.4.5@myapp/gems/activesupport-4.2.10/lib/active_support/lazy_load_hooks.rb:28:in `block in on_load'
/Users/myuser/.rvm/gems/ruby-2.4.5@myapp/gems/activesupport-4.2.10/lib/active_support/lazy_load_hooks.rb:27:in `each'
/Users/myuser/.rvm/gems/ruby-2.4.5@myapp/gems/activesupport-4.2.10/lib/active_support/lazy_load_hooks.rb:27:in `on_load'
/Users/myuser/.rvm/gems/ruby-2.4.5@myapp/gems/railties-4.2.10/lib/rails/railtie/configuration.rb:53:in `before_configuration'
/Users/myuser/Documents/workspace/myco/myapp/config/application.rb:113:in `<class:Application>'
/Users/myuser/Documents/workspace/myco/myapp/config/application.rb:9:in `<module:mycoWeb>'

What's the proper way to include/name my library?

according to the naming convention you should use module name as AppConfig

require 'yaml'

module AppConfig
  class AwsSecrets

    def self.load
      …
    end
  end
end
application.rb

config.eager_load_paths += %W( #{config.root}/lib/**/*.rb )

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