AssociationTypeMismatch:期待一个模型,但得到一个数组? Rails 3嵌套表单

时间:2013-03-18 16:12:29

标签: ruby-on-rails nested-forms type-mismatch

我有一个嵌套的表单,并且在另一个表单中。基本上我所拥有的是一堆成分,对于每种成分,我有一个数量和单位,存储在Entry和Ingredient之间的关联表中,称为EntryIngredient。使用我的以下JS,我可以添加动态字段,但是当我提交时,我收到了AssociationTypeMismatch错误。不知道为什么,参数对我来说很好("ingredients_attributes"=>{"0"=>{"name"=>"Salt", "entry_ingredients"=>{"quantity"=>"2.5", "unit"=>"tbspns"}, "_destroy"=>""}}}}),我错过了什么?实际上,我认为这可能是因为它发送为entry_ingredients而不是entry_ingredients_attributes,但我没有看到我在第二个嵌套表单中做了哪些不同的操作。这是第一个包含成分领域的表格:

  <div id="ingredients">
      <p>Ingredient List:</p>
      <%= f.fields_for :ingredients, @entry.ingredients.build do |builder| %>
      <%= render 'ingredient_fields', :f => builder %>
      <% end %>
  </div>
  <div id='add_ingredient'>Add Ingredient</div>
  <div class="actions">
    <%= f.submit %>

以下是entry_ingredients的代码:

<ul id="ingredient_list">
    <li>
      <%= f.label :name %>
      <%= f.text_field :name, :class => "ingredient_field" %>
      <%= f.hidden_field :_destroy, :class => "delete_ingredient" %>
        <%= f.fields_for :entry_ingredients, @entry.entry_ingredients.build do |builder| %>
        <%= render 'entry_ingredient_fields', :f => builder %>
        <% end %>
      <%= link_to "Remove", "#", :class => "remove_fields" %>
    </li>
</ul>

并且继续我的JS动态添加新成分:

$('form').on('click', '#add_ingredient', function(){
    count = $('#ingredient_list li').length;
    field = $('#ingredient_list li').first()
        .clone()                                    //clone the first element in the list
            .find('input')                          //find all its inputs
                .val('')                            //set all input values to blank
                    .end()
                        .find($('.ingredient_field'))
                            .prop({id: 'entry_ingredients_attributes_' + count + '_name', name: 'entry[ingredients_attributes][' + count +'][name]'  })
                                .end()
                                    .find($('.delete_ingredient'))
                                        .prop({id: 'entry_ingredients_attributes_' + count + '__destroy', name: 'entry[ingredients_attributes][' + count +'][_destroy]', value: 'false'  })
                                            .end()
                                                .find($('.ingredient_quantity'))
                                                    .prop({id: 'entry_ingredients_attributes_' + count + '_entry_ingredients_quantity', name: 'entry[ingredients_attributes][' + count +'][entry_ingredients][quantity]'})
                                                        .end()
                                                            .find($('.ingredient_unit'))
                                                                .prop({id: 'entry_ingredients_attributes_' + count + '_entry_ingredients_unit', name: 'entry[ingredients_attributes][' + count +'][entry_ingredients][unit]'})
                                                                    .end();
    $('#ingredient_list').append(field);
})

1 个答案:

答案 0 :(得分:1)

表单助手检查模型之间的关系,以确定如何提交参数。您是否在accepts_nested_attributes_for :entry_ingredients模型中添加了Ingredient