我有2種型號,Category
和LineItemTypes
創建或刪除協會與accepts_nested_attributes_for
已經有很多現有的都有,現在是他們相關的要求。每個類別都有很多LineItemTypes。
我已經Category
添加accepts_nested_attributes_for :line_item_types
我在窗體上使用hidden_field
創建的現有相關LineItemTypes
列表嘗試:如果我添加項目到該列表
- form_for @category do |form|
%ul#categorised
- form.fields_for :line_item_types do |line_item_types|
-categorised.each do |l|
%li
=l.description
=line_item_types.hidden_field :category_id
=form.submit
,我出現錯誤,指出無法找到該類別的LineItemType。我以爲accepts_nested_attributes_for想補充的關係,如果它不存在。或者只是爲了「創造」新記錄和修改現有關係,而不是創建新的關係。
a.update_attributes({:line_item_types_attributes => [{:id => 2767}, {:id => LineItemType.find(2).id}]})
ActiveRecord::RecordNotFound: Couldn't find LineItemType with ID=2 for Category with ID=1
任何想法,而不必寫東西來遍歷結果形式參數並創建關聯?或者更簡單的方法來實現這一點?
對不起,仍然有同樣的錯誤。我可以創建在該類別NEW line_item_types但我不能與現有的類別現有line_item_types關聯... – stuartc 2010-11-02 15:02:16