views:

426

answers:

4

Hi, 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.

A: 

The best way is to use a database table. Each row should contain a keyword and a value. Simples.

Mick Sharpe
OK, but how would you manage the different data types?For example, some settings will be boolean, others strings, others integers etc
Dan
store a JSON string.
Luke
+1  A: 

You can use database. Create a separate table "settings" that stores key/value parameters you need. The downside of this solution is a performance hit (querying DB every time you need a setting). To fix this issue you could you read/write through cache like "cache_money" or create your own by using "Rails.cache"

Zepplock
+1  A: 

Try this: Rails Settings http://github.com/Squeegy/rails-settings

or

this: http://github.com/ledermann/rails-settings

Yi-Ru Lin
A: 

I have used app_config gem for a while myself, but it fails with rails 2.3.9 (and probably also with rails 3.x), so I found this blog that mentions rails-setting and configuration, rails-setting stores values in DB, but configuration have namespaces built-in. I haven't tried them, but I think I will switch to rails-settings.

I notice now that the branch of rails-settings that Yi-Ru Lin mentions seems to be more featureful than the other rails-setting

Jarl

Jarl