Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails AssociationTypeMismatch:需要一个模型,但得到了一个数组?Rails 3嵌套表单_Ruby On Rails_Nested Forms_Type Mismatch - Fatal编程技术网

Ruby on rails AssociationTypeMismatch:需要一个模型,但得到了一个数组?Rails 3嵌套表单

Ruby on rails AssociationTypeMismatch:需要一个模型,但得到了一个数组?Rails 3嵌套表单,ruby-on-rails,nested-forms,type-mismatch,Ruby On Rails,Nested Forms,Type Mismatch,我有一个嵌套的表单,在这个表单中还有另一个。基本上,我拥有的是一堆配料,对于每种配料,我都有一个数量和单位,存储在条目和配料之间的关联表中,称为EntryingCredit。使用下面的JS,我可以添加动态字段,但是当我提交时,我得到了AssociationTypeMismatch错误。不知道为什么,参数对我来说很好(“成分属性”=>{“0”=>{“名称”=>“盐”,“输入成分”=>{“数量”=>“2.5”,“单位”=>“TBSPN”},“{u销毁”=>“}}}),我缺少什么?实际上,我认为这可能

我有一个嵌套的表单,在这个表单中还有另一个。基本上,我拥有的是一堆配料,对于每种配料,我都有一个数量和单位,存储在条目和配料之间的关联表中,称为EntryingCredit。使用下面的JS,我可以添加动态字段,但是当我提交时,我得到了AssociationTypeMismatch错误。不知道为什么,参数对我来说很好(
“成分属性”=>{“0”=>{“名称”=>“盐”,“输入成分”=>{“数量”=>“2.5”,“单位”=>“TBSPN”},“{u销毁”=>“}}}
),我缺少什么?实际上,我认为这可能是因为它作为
entry\u components
而不是
entry\u components\u 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 %>

表单帮助程序检查模型之间的关系,以确定如何提交参数。您是否在
配料
模型中包含了
接受嵌套的配料属性\u:entry\u配料

啊,您是对的,我在entry模型中包含了
接受嵌套的配料属性\u:entry\u配料
。呜呜声
<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>
$('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);
})