I was wondering how to add custom configuration variables to a rails application and how to access them in the controller, for e.g I wanna be able to define an upload_directory in the configuration files say development.rb and be able to access it in one of my controllers.

Secondly I was planning to have S3 support for uploads in my application, if i wanted to add a yaml file with the s3 access, secret key, how do I initialize it in my Rails App and how do I access the values that I have defined in that config file.

In Rails 3, Application specific custom configuration data can be placed in the application configuration object. The configuration can be assigned in the initialization files or the environment files -- say for a given application MyApp:

MyApp::Application.config.custom_config_variable = :my_config_setting

or

Rails.configuration.custom_config_variable = :my_config_setting

To read the setting, simply call the configuration variable without setting it:

It works for me. I put my environment specific configuration settings in environments/{environment}.rb, e.g., environments/development.rb. Make sure you restart your server after modifying. There may be more elegant techniques, but I'm upgrading an existing app which previously used ENV['XXX'] in the same file, and since I want to limit the amount of refactoring during the upgrade, this worked out well.
–
pdueyApr 26 '11 at 16:13

12

This is assignment but how do you access this value then?
–
kevzettlerAug 10 '11 at 16:39

7

If you look in the application.rb of a rails 3.x app it sais "Application configuration should go into files in config/initializers" as per this answer. There is plenty of documentation on how to access the variable (edgeguides.rubyonrails.org/configuring.html). e.g. in a file called some_variables.rb in the initializers folder place the variable as described above (replacing MyApp with the name of your app) then when you want to use it just call config.custom_config_variable You can of course use whatever variable name you like. @Jack Pratt you could edit a more complete answer?
–
jameswAug 31 '11 at 14:58

Beware that if you set something only in production, and try to access it in another environment, it would complain undefined method.
–
lulalalaMar 15 '12 at 10:45

I Tried this in rails 4.1 and I'm getting errors, "...method_missing': undefined method store' for #<Rails::Application::Configuration:0x007f9f735b7240>...". I was trying to add "config.store.works" config variable.
–
SpundunMar 19 '14 at 21:52

1

@Spundun You're getting that error, because Rails doesn't know what store is. In order to use the method in this answer, you can either rename your variable store_works, or create a config.store block with works defined therein.
–
Paul PettengillAug 21 '14 at 18:16

Thanks, I eventually figured that out. That these are nested hashes so I have to initialize empty hashes recursively if I'm to use dots in my config names.
–
SpundunAug 21 '14 at 18:48

and now you can use AUTHENICATION constant everywhere in your application:

#rails c production
:001> AUTHENTICATION['username'] => 'aa'

then add passwords.yml to .gitignore: echo /config/passwords.yml >> .gitignore, create an example file for your comfort cp /config/passwords.yml /config/passwords.example.yml and then just edit your example file in your production console with actual production values.