child_index的字段不会更改为nested_attributes

时间:2016-11-27 13:41:42

标签: javascript ruby-on-rails ruby-on-rails-4 ruby-on-rails-5

我使用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?

0 个答案:

没有答案