ActiveScaffold 抱怨关联
我正在使用 ActiveScaffold 创建管理 UI。
- 我有两个模型:帖子和评论。
- 一个帖子有很多评论,一条评论属于一个帖子。
- 有一个 validates_presences_of :text 验证 Comment 模型。
问题是,当我从管理 UI 创建新帖子而没有在子表单中创建新评论时,ActiveScaffold 会抱怨验证。如果我还创建与之关联的评论,我就可以创建该帖子,但这不是我想要的。
我可以从脚本/控制台手动创建新帖子。
什么给?
I'm using ActiveScaffold to create an Admin UI.
- I have two models: Post and Comments.
- A Post has-many Comments, and a Comment belongs-to a post.
- There's a validates_presences_of :text validation the Comment model.
The problem is that when I create a new Post from the Admin UI without creating a new Comment in the subform, ActiveScaffold complains about the validation. I can create the Post if I also create a Comment associated with it, which is not what I Want.
I can create a new Post manually from script/console.
What gives?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许 ActiveScaffold 的东西你想为每个帖子至少创建一个评论。我在 has-one 中遇到过这个问题……看起来 ActiveScaffold 在 has-many 情况下足够聪明,但谁知道呢。
以下是我如何解决 has-one 的问题(这也是我想要的用户体验):
当然 :select 对注释没有意义,但您可以研究类似的 UI 更改,告诉它不要尝试坚持表单内联(如果确实发生了这种情况)
Maybe ActiveScaffold things that you want to create at least one Comment per Post. I've encountered this problem with a has-one… it seems like ActiveScaffold would be smart enough in the has-many case, but who knows.
Here's how I solved it for has-one (and is the UX I wanted anyway):
of course :select won't make sense for comments, but you could look into a similar UI change, telling it to not try to stick the form inline (if that is indeed what's happening)
您希望在创建/编辑帖子时防止默认情况下(尝试)创建空白评论记录。幸运的是,AS 有一个 API::Column.show_blank_records 选项来控制此行为:
这将要求用户在创建/编辑帖子时单击“添加新”按钮来创建新的评论记录,因此不会进行验证检查在空白记录上运行。
请参阅 https://github.com/activescaffold/active_scaffold/wiki/API:-Column< /a>
You want to prevent the (attempted) creation of a blank Comment record by default when creating/editing a Post. Luckily AS has an API::Column.show_blank_records option to control this behavior:
This will require the user to click the 'Add New' button to create a new Comment record when creating/editing a Post, so the validation check won't be run on a blank record.
See https://github.com/activescaffold/active_scaffold/wiki/API:-Column