使用 MVC/Padrino ruby​​ 1.8.x 进行复杂的哈希编辑

发布于 2024-09-07 02:20:40 字数 449 浏览 7 评论 0 原文

我是 MVC 新手。我使用 Padrino 与 MongoMapper 和 Haml 来尝试创建此应用程序。

我有一个项目数据库,每个项目都有一个与其关联的哈希值,称为“params”。该哈希有一些必需的密钥,但大部分是任意/随机密钥。某些键具有一组有限的允许值。

例如:

item.params["password"] 没有一组有限的可能值。

item.params["color"] 必须是 %w{red blue green} 之一 为该

哈希中的项目创建编辑器的最佳方法是什么?我想要一个用于自由格式值的文本字段和用于有限值的菜单。我还希望这些菜单将“创建新”作为底部选择,因此我不能只对所有有限值集进行硬编码。

我能想到的最好的办法是在数据库中有一个新的集合(表),它只是默认值的哈希值。如果您的密钥不在该哈希中,您会看到一个文本框。

看来必须有更好的方法。

I'm new to MVC. I'm using Padrino with MongoMapper and Haml to try to create this application.

I have a database of items, each of which has a hash associated with it called 'params'. This hash has some required keys, but mostly arbitrary/random keys. Some of the keys have a finite set of allowable values.

For example:

item.params["password"] does NOT have a finite set of possible values.

item.params["color"] must be one of %w{red blue green}

What's the best way to create an editor for the items in this hash? I'd want a text-field for free-form values and menus for finite values. I'd also like these menus to have "Create New" as the bottom choice, so I can't just hard-code all of the finite-value sets.

The best thing I can come up with for this is to have a new collection(table) in the DB that's just a Hash of default values. If your key isn't in this hash, you get a text-box.

Seems like there has to be a better way though.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

网白 2024-09-14 02:20:40

您的问题似乎与此非常相似: mongodb 和 mongomapper

我认为您可能想要构建一组“类型” ”与“度量单位”...所以你的“类型”集合将有这样的条目:

{"_id" : "password", "display" : "password"}
{"_id" : "user_name", "display" : "text", "restrictions" : ["max_length": 20]}
{"_id" : "eye_color", "display" : ["red","blue","green"], "restrictions" : ["single"]}

所以显然,渲染这些东西的逻辑都必须在 Ruby 端。然而,在 RDBM 中尝试过这些东西后,这肯定会更容易。

是的,这里的弱点是您最终可能会为每个字段查询一次“types”集合。因此,如果您显示 10 个字段,则您将进行 10 次查询。这听起来可能有点蹩脚,但这基本上就是 MySQL 数据库正在做的事情。您可能希望为“类型”集合构建一个“缓存”层,因为您可能希望它永久“在内存中”。

Your questions seems quite similar to this one: mongodb and mongomapper

I think that you'll probably want to build a collection of "types" with "units of measure"... so your "types" collection would have entries like this:

{"_id" : "password", "display" : "password"}
{"_id" : "user_name", "display" : "text", "restrictions" : ["max_length": 20]}
{"_id" : "eye_color", "display" : ["red","blue","green"], "restrictions" : ["single"]}

So obviously, the logic for rendering this stuff will all have to be on the Ruby side. However, having tried this stuff in RDBMs, this is definitely going to be easier.

Yes the weakness here is that you could end up querying the "types" collection once for each field. So if you're showing 10 fields, you make 10 queries. It may sound a little lame, but it's basically what your MySQL Database is doing anyways. You'll probably want to build a "caching" layer for the "types" collection as you'll probably want it permanently "in-memory".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文