Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define custom configuration variables in rails

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 want to 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.

like image 968
Shiv Avatar asked Sep 20 '09 04:09

Shiv


People also ask

Where to find Rails configuration?

You probably know that you can configure Rails in config/application. rb and config/environments/development. rb etc. But you can also leverage that for configuring your own custom settings for your application.

What is application RB in Rails?

The configuration file config/application. rb and environment-specific configuration files (such as config/environments/production. rb ) allow you to specify the various settings that you want to pass down to all of the components. For example, you could add this setting to config/application.rb file: config.

What is the purpose of environment RB and application RB file?

In the environment. rb file you configure these run-levels. For example, you could use it to have some special settings for your development stage, which are usefull for debugging. The purpose of this file is to configure things for the whole application like encoding.


1 Answers

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:

Rails.configuration.custom_config_variable => :my_config_setting 

UPDATE Rails 4

In Rails 4 there a new way for this => http://guides.rubyonrails.org/configuring.html#custom-configuration

enter image description here

like image 164
Jack Pratt Avatar answered Sep 28 '22 01:09

Jack Pratt