Ruby on Rails - 存储应用程序配置 [英] Ruby on Rails - Storing application configuration

查看:20
本文介绍了Ruby on Rails - 存储应用程序配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相对简单的 Rails 应用程序,我想存储管理员用户可以在应用程序运行时更改的各种配置设置,例如,允许对帖子发表评论或更改日期的显示格式.

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.

我知道我可以在 environment.rb 文件中存储常量等,但是这些似乎只有在服务器重新启动时才会加载.

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?

感谢任何建议.

谢谢.

推荐答案

可以使用数据库.创建一个单独的表设置",用于存储您需要的键/值参数.此解决方案的缺点是性能下降(每次需要设置时都查询数据库).要解决此问题,您可以通过像cache_money"这样的缓存读取/写入或使用Rails.cache"创建自己的缓存

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"

这篇关于Ruby on Rails - 存储应用程序配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆