var table = $('#products_table').DataTable(); <% @pre_purchases.each do |pre_purchase| %> var newRow = $('<%= j render (pre_purchase) %>'); x = $('#products_table').dataTable(); newRow.find('td:eq(0)').html(newRow.find('td:eq(0)').html().replace('#', x.fnGetData().length + 1)); table.row.add($(newRow)).draw(); <% end %> $('#dialog').modal('toggle'); calculateTotals();