I’m a simple guy— I just want a YAML config file that’s accessible from controllers. That’s all.
First, I created config/app_config.yml, which contains the config values I want. So far, so good.
Then, in I created lib/app_config.rb, which contains:
module AppConfig
def self.config
@@config ||= {}
end
def self.config=(config)
@@config = config
end
end
Finally, I created config/initializers/load_app_config.rb, which contains:
# the ./ is necessary for some reason, though no examples online use it...
require './lib/app_config'
AppConfig.config = YAML.load_file("#{Rails.root.to_s}/config/app_config.yml")
Should I skip load_app_config.rb altogether, and assign AppConfig.config some other way (perhaps inside of the module itself)?
It seems I’m making it too complicated..
You are making it a little more complicated than it needs to be. You only need 2 things:
Your conifg: /config/config.yml
(should look something like this, of course these are just placeholders)
Then your initializer: /config/initializers/app_config.rb
Now you can access anything from you config from anywhere via AppConfig. For example to grab the host name in whatever environment you are in would be:
If we were running locally, that value would be “localhost:3000”