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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With