nested form collection_select new value overwriting previous selected values

Posted by bharath on Stack Overflow See other posts from Stack Overflow or by bharath
Published on 2012-06-02T09:56:15Z Indexed on 2012/06/02 10:41 UTC
Read the original article Hit count: 354

Nested form

<%= nested_form_for(@bill) do |f| %>

<p><%= f.link_to_add "Add Product", :bill_line_items %> </p>

Partial of bill line items

<%= javascript_include_tag 'bill'%>

<%= f.hidden_field :bill_id %>

<%- prices = Hash[Product.all.map{|p| [p.id, p.price]}].to_json %>

<%= f.label :product_id %>
<%= f.collection_select :product_id ,Product.all,:id,:name, :class => 'product',  :prompt => "Select a Product", input_html: {data: {prices: prices}}%> <br/ >


<%= f.label :price, "price"%> 
<%= f.text_field :price, :size=>20, :class =>"price" %><br/>

bill.js

jQuery(document).ready(function(){
        jQuery('.product').change(function() {
            var product_id = jQuery(this).val();
            var price = eval(jQuery(this).data("prices"))[product_id];
            jQuery('.price').val(price);
        });
    });

Rails 3.2 Issue: Second click on Add Product & on selecting the product When we select the second product the price of second product is overwriting on the price of the first selected product. Request help.

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about ruby-on-rails-3