【发布时间】:2011-06-28 21:19:48
【问题描述】:
我有点陷入概念性问题。假设 Post、Tag 和 User 的 [abstracted] 设置如下:
Post belongs_to Tag
Tag has_many Posts
User has_many Tags,
has_many Posts
用户只能使用他的关联标签之一来标记帖子。
在新的帖子表单视图中,我现在有以下用于选择标签的选项:
f.collection_select :tag_id, current_user.tags, ...-
f.collection_select :tag_id, @tags,并在控制器的新操作中:@tags = current_user.tags
问题:什么是概念上正确的选项?
从 MVC 的角度来看,我绝对倾向于使用第二个选项。视图知道它应该在 collection_select 中呈现的标签与用户相关联,这似乎是不对的(更具体地说,是当前用户!)。
但是,在网络上的official api documentation for collection_select 和一些other tutorials 中,我看到了这样的内容:
collection_select(:post, :author_id, Author.all, ...)
这显然有利于第一个选项。在这种方法的亲网站上,我不需要在控制器的创建操作中重新定义@tags,以防帖子的保存操作失败并且我想再次呈现新操作。
提前感谢您的建议。
【问题讨论】:
标签: ruby-on-rails forms selection helpers form-helpers