我使用nested_attributes有很多关联。嵌套的属性是动态添加的。
该关联是Invoice has_many Items
控制器代码:
def new
@invoice = Invoice.new
@invoice.items.build
end
new.html.erb
<%= simple_form_for(@invoice,:format => :js,:method => :post, :post => true,:url => invoices_path, :required => true) do |f|%>
<%= f.fields_for :items do |builder| %>
<%= builder.text_field :fullname %>
<% end %>
<%= f.fields_for :items do |builder| %>
<%= builder.text_field :fullname %>
<% end %>
<script type="text/javascript">
$('#btnAddItemtoInvoice').on('click', function(){
$('.exp').append("<%= escape_javascript(render partial: 'item_fields.html.erb', locals: {f: f}) %>")
})
</script>
<% end %>
对于前两行,子索引正确递增,但对于来自partials的行不会递增。我得到的子索引是:0,1,2,2,2等等。
部分:_item_fields.html.erb
<%= f.fields_for :items do |builder| %>
<%= builder.text_field :fullname %>
<% end %>
为什么child_index没有递增?如何在这里增加child_index?