0
我轨得到了与嵌套问题fields_for,我的产品列表以及每个产品可以有多个图片,所以我用Angular.js来完成这个任务,我跟着这个教程https://tatey.com/2013/01/13/adding-and-removing-children-with-rails-nested-forms-and-angularjs/,但名称的投入没有合适的名称,因为指数并没有在第一个参数仅在第二递增,像这样:如何在嵌套fields_for上设置正确的索引?
一是产品有两个图像,所以这是分配给每个输入的名称
catalog[products_attributes][0][images_products_attributes][0][url_image]
catalog[products_attributes][0][images_products_attributes][1][url_image]
第二个产品有一个图像,这是属性
catalog[products_attributes][0][images_products_attributes][0][url_image]
正如你可以看到第二个产品具有指数在products_attributes,但它应该是
这是我的代码
<div ng-repeat="product in products">
<div class="product">
<%= f.fields_for :products, Product.new do |product|%>
<div class="field">
Producto {{$index}}
<%= product.text_field :name, placeholder: "Nombre"%>
</div>
<div class="field">
<%= product.text_field :description, placeholder: "Descripción" %>
</div>
<div class="field">
<%= product.number_field :price, placeholder: "Precio" %>
</div>
<div class="field">
<%= product.check_box :available %>
</div>
<input type="button" name="name" value="+" ng-click="addImageToProduct($index)">
<div class="images">
<div class="image" ng-repeat="image in product.images">
<%= product.fields_for :images_products, ImagesProduct.new, child_index: "{{$index}}" do |image|%>
<div class="field">
{{$index}}<%= image.file_field :url_image%>
</div>
<%end%>
<input type="button" name="name" value="-" ng-click="removeImageToProduct($parent.$index, $index)">
</div>
</div>
<%end%>
</div>
<input class="step-button remove" type="button" name="name" value="Quitar" ng-click="removeProduct($index)">
</div>
太谢谢你了!这工作!我一直在尝试,但这是完美的! – peternerd