user1977201
user1977201

Reputation: 61

rails: calculate field price dynamically

I am building a create new product form. I have fields foodio_price and selling_price. Here what I want to do is, when user enters foodio_price, selling_price should be calculated at back end and displayed along with it. My form view is:

 <%= nested_form_for @product do |f| %>
<%= f.label :name, "Product Name"%>
    <%= f.text_field :name %>
    <%= f.label :foodio_price %>
    <%= f.text_field :foodio_price %>
    <%= f.label :selling_price, "Selling Price"%>
    <%= @product.selling_price %>
    <%= f.submit %>
 <% end %>

In product model, selling price is calculated. Can anybody tell how to display selling_price dynamically as user enters foodio_price without reloading form?

Upvotes: 0

Views: 855

Answers (1)

Salil
Salil

Reputation: 9722

Using JQuery:

$(document).ready(function(){
      $(':input[name*="foodio_price"]').change(function() {
        var fprice = $(this).val();
        // Obtain the selling price from server
        $.get("my_server_url", { fprice: fprice })
        .done(function(data) {
            $(':input[name*="selling_price"]').val(data);
        });
      });
}

Upvotes: 1

Related Questions