picardo
picardo

Reputation: 24886

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.

Upvotes: 1

Views: 303

Answers (2)

Sam 山
Sam 山

Reputation: 42865

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.

Upvotes: 2

Srdjan Pejic
Srdjan Pejic

Reputation: 8202

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.

Upvotes: 1

Related Questions