Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails - Storing application configuration

Tags:

I have a relatively simple Rails app and I would like to store various configuration settings that administrator users can change whilst the application is running, for example, allowing comments on posts or changing the display format of the date.

I know I can store constants etc in the environment.rb file, however these appear to be loaded only when the server is restarted.

Is there an alternative place I can define this information or would it be better to keep it in the database?

Any advice appreciated.

Thanks.

like image 746
Dan Avatar asked Jan 24 '10 17:01

Dan


People also ask

Where is Rails application config?

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

You could use the rails-settings-cached gem which is a fork of the rails-settings gem (linked by Yi-Ru Lin in another answer).

Once setup, you'll be able to do things such as:

Setting.foo = 123
Setting.foo # returns 123

You can also manage settings on models, ex:

user.settings.color = :red
user.settings.color # returns :red
like image 117
mbillard Avatar answered Nov 05 '22 21:11

mbillard