我在这里遇到了一个概念问题。假设 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 中呈现的标签与用户相关联似乎并不正确(更具体地说,当前用户!)。
但是,在collection_select 的官方 api 文档和网络上的其他一些 教程中,我看到了这样的内容:
collection_select(:post, :author_id, Author.all, ...)
这显然有利于第一种选择。在这种方法的亲网站上,我不需要在控制器的创建操作中重新定义@tags,以防帖子的保存操作失败并且我想再次呈现新操作。
提前感谢您的建议。