假設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