Javascript 嵌套\u属性的子\u索引字段未更改
我将嵌套的_属性用于具有许多关联的对象。嵌套属性是动态添加的 关联为发票包含多个项目 控制器代码:Javascript 嵌套\u属性的子\u索引字段未更改,javascript,ruby-on-rails,ruby-on-rails-4,ruby-on-rails-5,Javascript,Ruby On Rails,Ruby On Rails 4,Ruby On Rails 5,我将嵌套的_属性用于具有许多关联的对象。嵌套属性是动态添加的 关联为发票包含多个项目 控制器代码: 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, :require
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 %>
:js,:method=>:post,:post=>true,:url=>发票路径,:required=>true)do | f |%>
$('btnAddItemtoInvoice')。在('click',function(){
$('.exp')。追加(“”)
})
对于前两行,子索引正确递增,但对于部分中的行,子索引不会递增。我得到的儿童指数是:0,1,2,2,2等等。
部分:_item_fields.html.erb
<%= f.fields_for :items do |builder| %>
<%= builder.text_field :fullname %>
<% end %>
为什么child_索引不递增?我如何在这里增加child_索引 我认为,在您使用的方式中,分部生成一次,然后每次都使用。尝试使用“单击”从服务器请求新发票,然后创建其HTML并附加。我不希望单击服务器仅添加新行,然后您可以从字段中排除id并仅在保存时创建它,然后在附加时您将根本没有id-服务器仅在获得要保存的数据时才会生成它