Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7652199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:42:22+00:00 2026-05-31T11:42:22+00:00

I have a big problem when using Ryan Bates’ excellent Nested_form gem. In fact,

  • 0

I have a big problem when using Ryan Bates’ excellent Nested_form gem. In fact, it works like a charm in my view that is a nested one to dynamically add and remove fields.

The issue is that I need to perform some calculation when the user input a value in a field like an onchange event. The problem is that the nested_form gem gives the same id to all the new added lines and then it’s impossible to select it to perform some tasks… I must confess that I’m not a Jquery guru at all…

Is there somebody to help me with this one.

Here is the code of the main view:

<%= nested_form_for @pinvoice do |f| %>
<% if @pinvoice.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@pinvoice.errors.count, "error") %> prohibited this pinvoice from being saved:</h2>
<ul>
<% @pinvoice.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :contact %>
<%= f.text_field :contact %>
<%= f.label :date_facture %>
<%= f.date_select :date_facture %>
<%= f.label :montant_total %>
<%= f.text_field :montant_total %>
<br />
<br />
</p>
<%= f.fields_for :pinvlines %>
<p><%= f.link_to_add "add one", :pinvlines%>
<p>
<div class="actions">
<%= f.submit %>
</div>
</p>
<% end %>

and the code of the “nested partial:

<%= javascript_include_tag 'javascript_pinvlines_fields_new' %>
<div class="fields">
<%= f.label :description %>  
<%= f.text_field :description %>
<%= f.label :compte_id %>
<%= f.collection_select(:compte_id, @compte, :id, :nom, {:prompt => "Type de charge"}) %>
<%= f.label :quantite %>
<%= f.text_field :quantite, :size => 10 %>
<%= f.label :prix_unitaire %>
<%= f.text_field :prix_unitaire, :size => 10 %>
<%= f.label :montant_HTVA %>
<%= f.text_field :montant_HTVA, :size => 10 %>
<%= f.link_to_remove "remove"%>
</p>
</div>

What would be the code to automatically calculate the field called “montant_HTVA” that is “quantite” multiplied by “prix_unitaire” using Jquery and working with all the new added lines too….

Thanks a lot for your help.

Here is the par of the html code with 2 lines coming by default withe the view + 2 added lines:

<label for="pinvoice_pinvlines_attributes_0_quantite">Quantite</label>
<input name="pinvoice[pinvlines_attributes][0][quantite]" onchange="calculateMontant();" size="10" type="text" />
<label for="pinvoice_pinvlines_attributes_0_prix_unitaire">Prix unitaire</label>
<input id="pinvoice_pinvlines_attributes_0_prix_unitaire" name="pinvoice[pinvlines_attributes][0][prix_unitaire]" size="10" type="text" />
<label for="pinvoice_pinvlines_attributes_0_montant_HTVA">Montant htva</label>
<input id="montant_HTVA1" name="pinvoice[pinvlines_attributes][0][montant_HTVA]" size="10" type="text" />
<input id="pinvoice_pinvlines_attributes_0__destroy" name="pinvoice[pinvlines_attributes][0][_destroy]"   type="hidden" value="false" /><a href="javascript:void(0)" class="remove_nested_fields">remove</a>
</p>
</div></div><div class="fields"><script src="/javascripts/javascript_pinvlines_fields_new.js"   type="text/javascript"></script>
<div class="fields">
<label for="pinvoice_pinvlines_attributes_1_description">Description</label>  
<input id="pinvoice_pinvlines_attributes_1_description" name="pinvoice[pinvlines_attributes][1][description]" size="30" type="text" />
<label for="pinvoice_pinvlines_attributes_1_compte_id">Compte</label>
<select id="pinvoice_pinvlines_attributes_1_compte_id" name="pinvoice[pinvlines_attributes][1][compte_id]">    <option value="">Type de charge</option>
<option value="1">Caisse</option>
<option value="2">UBS</option>
<option value="3">Ventes</option>
<option value="4">Marketing</option>
<option value="7">Capital</option>
<option value="8">TVA sur ventes</option>
<option value="9">TVA sur achats</option></select>
<label for="pinvoice_pinvlines_attributes_1_quantite">Quantite</label>
<input name="pinvoice[pinvlines_attributes][1][quantite]" onchange="calculateMontant();" size="10" type="text" />
<label for="pinvoice_pinvlines_attributes_1_prix_unitaire">Prix unitaire</label>
<input id="pinvoice_pinvlines_attributes_1_prix_unitaire" name="pinvoice[pinvlines_attributes][1][prix_unitaire]" size="10" type="text" />
<label for="pinvoice_pinvlines_attributes_1_montant_HTVA">Montant htva</label>
<input id="montant_HTVA2" name="pinvoice[pinvlines_attributes][1][montant_HTVA]" size="10" type="text" />        <input id="pinvoice_pinvlines_attributes_1__destroy" name="pinvoice[pinvlines_attributes][1][_destroy]" type="hidden" value="false" /><a href="javascript:void(0)" class="remove_nested_fields">remove</a>
</p>
</div></div>
<p><a href="javascript:void(0)" class="add_nested_fields" data-association="pinvlines">add one</a>
<p>
<div class="actions">
<input id="pinvoice_submit" name="commit" type="submit" value="Create Pinvoice" />
</div>
</p>
</form><div id="pinvlines_fields_blueprint" style="display: none"><div class="fields"><script  src="/javascripts/javascript_pinvlines_fields_new.js" type="text/javascript"></script>
<div class="fields">
<label for="pinvoice_pinvlines_attributes_new_pinvlines_description">Description</label>  
<input id="pinvoice_pinvlines_attributes_new_pinvlines_description" name="pinvoice[pinvlines_attributes] [new_pinvlines][description]" size="30" type="text" />
<label for="pinvoice_pinvlines_attributes_new_pinvlines_compte_id">Compte</label>
<select id="pinvoice_pinvlines_attributes_new_pinvlines_compte_id" name="pinvoice[pinvlines_attributes] [new_pinvlines][compte_id]"><option value="">Type de charge</option>
<option value="1">Caisse</option>
<option value="2">UBS</option>
<option value="3">Ventes</option>
<option value="4">Marketing</option>
<option value="6">Loyer</option>
<option value="7">Capital</option>
<option value="8">TVA sur ventes</option>
<option value="9">TVA sur achats</option></select>
<label for="pinvoice_pinvlines_attributes_new_pinvlines_quantite">Quantite</label>
<input name="pinvoice[pinvlines_attributes][new_pinvlines][quantite]" onchange="calculateMontant();" size="10" type="text" />
<label for="pinvoice_pinvlines_attributes_new_pinvlines_prix_unitaire">Prix unitaire</label>
<input id="pinvoice_pinvlines_attributes_new_pinvlines_prix_unitaire" name="pinvoice[pinvlines_attributes][new_pinvlines][prix_unitaire]" size="10" type="text" />
<label for="pinvoice_pinvlines_attributes_new_pinvlines_montant_HTVA">Montant htva</label>
<input id="montant_HTVA3" name="pinvoice[pinvlines_attributes][new_pinvlines][montant_HTVA]" size="10" type="text" />
<input id="pinvoice_pinvlines_attributes_new_pinvlines__destroy" name="pinvoice[pinvlines_attributes][new_pinvlines][_destroy]" type="hidden" value="false" /><a href="javascript:void(0)" class="remove_nested_fields">remove</a>
</p>
</div></div></div>
<a href="/pinvoices">Back</a>
</body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T11:42:24+00:00Added an answer on May 31, 2026 at 11:42 am

    I’ve changed the way to perform my calculation integreting it directly at a Rails level.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using gettext in my PHP code, but I have a big problem.
I am in a big problem ..i have compiled my c files using linux
I have a big problem using highcharts, because I have been trying for hours
i have a big problem here..i am using Microsoft charting controls in my asp.net
I have a big problem using TBXML . When I use: + (NSString*) textForElement:(TBXMLElement*)aXMLElement
I have a big problem. I created a WCF service.My POST declaration looks like
I have a problem, where i have big list of number pairs. something like
I have a big problem writing a small piece of code using JS/jQuery (don't
I really seem to have a big problem here. I'm using MySQL to store
I'm using sqlalchemy. The question is simple, but I have a big problem, because

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.