使用户可以通过 Web 表单访问应用程序范围的设置
我整个下午都在四处寻找,试图找到在 Rails 中设置用户可修改的应用程序范围设置的主要约定……到目前为止还没有任何结果。
我正在开发一个简单的应用程序,该应用程序旨在由一个组织使用,因此不需要用户模型,只有一个管理员。该管理员需要能够修改某些站点范围的首选项,例如徽标、配色方案、标语等。
在 Rails 3.1 中创建此类应用程序范围的设置并使它们易于访问的最佳实践是什么最终用户?您可以链接到的任何示例应用程序都会获得奖励积分。
I've been looking around all afternoon to try and find the dominant convention for setting up user-modifiable application-wide settings in Rails... no dice so far.
I'm working on a simple application which is intended to be used by one organization, so there's no need for a user model, there's only a single administrator. That administrator needs to have the ability to modify certain site-wide preferences, things like the logo, color scheme, tagline, etc.
What's the best practice for creating this kind of application-wide settings in Rails 3.1, and making them easily accessible to the end-user? Bonus points for any example apps you can link to.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
存储可编辑的应用程序范围设置的主要约定似乎是键值存储的概念,由 ActiveRecord 或其他机制支持。而且,据我所知,至少有两种很好的策略来存储应用程序范围的设置,具体取决于您的需求。
如果您想要一种通用方法,但又非常灵活地定义几个可以与 AR 模型关联的(非)范围设置,您可以使用 Rails-Settings (或其缓存版本Rails-设置-缓存)。我还没有尝试在 Rails 3.1 中使用该插件,但它在 3.0 上运行良好。它允许您拥有以下功能:
如果您想要一种强大的方法,具有单表继承并允许您在某些设置中执行验证,就像使用实际的 AR 记录一样,您可以使用这个很好的 文章,作者:Jeff Dean,它将引导您完成整个过程。通过这种方式,您可以通过将设置分组为子类来确定设置的范围,并且您可以拥有以下内容:
而且我猜想,通过一些简单的调整,您甚至可以在某些中拥有
has_many
和belongs_to
关联您的设置(例如可变大小的电话号码或电子邮件列表)。就我个人而言,我更喜欢后一种方法(当设置是一个大问题时),因为它可以让您更好地控制存储的设置,并保持代码干净和干燥,从而允许您遵循 MVC 模式。
The dominant convention to store editable app-wide settings seems to be the concept of a key-value store, backed either by ActiveRecord or other mechanisms. And, as far as I know, there are at least two nice strategies for storing your app-wide settings, depending on your requisites.
If you want a generic approach, yet extremely flexible for defining a couple of (not) scoped settings that can be in association with AR Models, you have Rails-Settings (or its cached version Rails-Settings-Cached). I haven't tried using the plugin in Rails 3.1 but it works well on 3.0. It allows you to have things like:
In case you want a robust approach, with Single-Table-Inheritance and allowing you to perform validations in certain settings as you would with actual AR Records, you have this nice article, written by Jeff Dean, which steps you through the process. This way you scope settings by grouping them into subclasses and you can have things like:
And I guess that with some simple tuning you can even have
has_many
andbelongs_to
associations in some of your settings (like a variable-sized list of phone numbers or e-mails).Personally I prefer the latter approach (when settings are a big issue) because it gives you more control over the settings you store and keeps your code clean and DRY, allowing you to follow the MVC pattern.
我通常在管理部分设置一个带有一些基本脚手架的属性模型 - 然后在需要时调用相关属性“字段”,例如 Property.find(:field => "EARLIEST_DATE:YEAR") ,它将具有用户可设置的值。
属性可能不是数据库表的最佳名称(往往认为保留名称冲突的可能性太大) - 但您明白了。优点是您可以设置范围来访问用户设置的值。
I generally set up a Properties model with some basic scaffolding in the admin section - then call the relevant Property 'field' where required, say Property.find(:field => "EARLIEST_DATE:YEAR") which would have a user settable value.
Properties might not be the best name for a database table (tend to think there's too much chance of a reserved name collision somewhere down the line) - but you get the idea. Advantage is you can set up scopes to access the values set by the user.