Greetings! I am trying to calculate the price per item, to later add them, however, is only able to calculate an item, I have honestly had problems to assign a unique and dynamic id, so that each item has its own result, since only the first item is able to calculate, and seeing it through the browser console, I see that all the items share the same id, for now, this code that I have, I would greatly appreciate could suggest a better idea, now I would like to take the idea of assigning them the second in which it was created, however, they all share the same second that is assigned to the id, since they inherit the same variable, but I know that some correlative number or something similar could be added that can differentiate them dynamically, I share my code below:
inputs / form.html.erb
<%= form_for @input do |f| %>
<div class="field">
<%= f.label :invoice %> <br>
<%= f.text_field :invoice %>
</div>
<div class="field">
<%= f.label :provider %> <br>
<%= f.text_field :provider %>
</div>
<table border="1">
<thead>
<th>Producto</th>
<th>Cantidad</th>
<th>Precio</th>
<th>Utilidad</th>
</thead>
<tbody>
<%= f.fields_for :input_items do |item| %>
<%= render "input_item_fields", f: item %>
<% end %>
</tbody>
</table>
<div class='links'>
<br><%= link_to_add_association 'add product', f, :input_items, class: "btn btn-secondary" %>
</div>
<hr>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
inputs / _input_item_fields.html.erb
<% @t = Time.now.strftime("%S") %>
<tr>
<td><%= f.number_field :product_id %></td>
<td><%= f.text_field :quantity, id: "cantidad_#{@t}" %></td>
<td><%= f.text_field :price, id: "precio_#{@t}" %></td>
<td><%= f.number_field :utility %></td>
<td><input type="text" id="resultado_<%= @t %>" readonly></td>
</tr>
<script>
$('#cantidad_<%= @t %>, #precio_<%= @t %>').keyup(function(){
var cant = parseFloat($('#cantidad_<%= @t %>').val()) || 0;
var price = parseFloat($('#precio_<%= @t %>').val()) || 0;
var result = cant*price
$('#resultado_<%= @t %>').val(result);
})
</script>