Where to store configuration values?

Where is the best place to store configuration values ​​(keys, passwords, or just configuration values) in a Ruby on Rails application? I searched a lot and did not find an answer that I thought was right.

+4
source share
3 answers

Supposedly you read a guide on guides for setting up applications ? In addition, each pearl usually provides an initialization / configuration file, if necessary, in the post-installation generator and documents it in README.

+2
source

If it is related to passwords, I would recommend that you store the data in an external file so that you can not transfer sensitive data to your repository. Then you can load the data into memory by reading the file in the initializer, for example:

config / my_secrets.yml:

development: password: abcdefgh test: password: abcdefgh 

configurations / Initializers / load_my_config.rb:

 MY_CONFIG = YAML.load_file("#{Rails.root.to_s}/config/my_secrets.yml")[Rails.env] 

Now you can access password in any environment by accessing MY_CONFIG['password'] . The production password can be stored exclusively on the server (and in another safe place).

For insensitive data, I would just put the data directly in the initializer.

+3
source

passwords (for the application) should be processed using self-identification / authorization, such as Devise, Clearance, authLogic, etc.

The variables that you want to receive through the ruby ​​code of this request to the application can be stored in global variables. Fixed level values ​​can be stored in constants. In rails, since controllers inherit from application_controller, you can define class level constants there. Check the Ruby language for exact inheritance rules for these variable variable types.

In fact, it’s best not to do this at all or to avoid it whenever possible, and many good programmers avoid it like a plague. This means that your code blocks are not encapsulated - actions now depend on external values, and this increases the connection, forcing many elements to depend on one thing that can change outside their area.

+3
source

All Articles