假设Post
- Comment
模型嵌套的资源:form_for with association - 如何提供父母ID?
resources :posts do
resources :comments
end
应如何app/views/comments/_form.html.haml
(ERB也将这样做)的样子,这样它也提供了邮寄到注释附加到的ID?
目前只有一种方法,我知道是手动添加隐藏的输入与职位ID。它对我来说很脏。
有没有更好的方法?我期望导轨能够理解嵌套的资源并自动将post_id
作为隐藏输入。
= form_for [@post, @comment] do |f|
.field
f.label :body
f.text_field :body
hidden_field_tag :post_id, @post.id
.actions
= f.submit 'Save'
编辑:使用Mongoid,没有ActiveRecord的。
谢谢。
,如果你最终不得不经常为此,inherited_resources:
有关嵌套的表格/模型的更多信息,我建议看以下Railscasts宝石自动化为你的很多关联管理: https://github.com/josevalim/inherited_resources – Dominic 2010-11-13 18:36:09
它应该与Mongoid一起使用以及? – 2010-11-13 18:38:52