我有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