What is the best way to manage configurations in Ruby on Rails?
I have an app that has a lot of api keys and setting to be managed and开发者_开发技巧 they are loaded using an initializer. BEcause they are different for each environment, I can't set a constant in the environment.rb file. (I tried that; didn't work.) So I started comment-and-uncomment by hand before every deployment, which is tedious. I'm wondering what would be a best practice for a situation where you have to deal with multiple configuration settings loaded in an initializer.
In your environment.rb you can set your keys if the request is coming from local:
Rails::Initializer.run do |config|
if local_request?
CONSTANT1 = 10
else
CONSTANT1 = 20
end
end
What this will do is check if you are on localhost
which is localhost:3000
, your development environment. If so then the if/else statement will pick which constant to set.
another option would be to set the constants in your environments folder which is probably a better idea
so for your production constants put them in config/environments/production.rb
.
and for your development constants put them in config/environments/development.rb
.
You should be setting these constants in initializer files, which you can create under the initializer directory. Then, use the environment-specific config files that are under the environments directory to set them per environment.
精彩评论