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 6381015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:20:31+00:00 2026-05-25T02:20:31+00:00

I am trying to build an estimate form that supports multiple line items. After

  • 0

I am trying to build an estimate form that supports multiple line items. After completing an estimate line item by filling in the following fields:

  • properties
  • height
  • letter_quantity

I would like to query a database table to get a unit price to multiply by letter_quantity and display the line item’s cost. My thought is to execute the query and display the line item’s cost onBlur from the letter_quantity field.

Here is my form:

<%= nested_form_for @estimate do |f| %>

    <%= f.label :date %><br />
    <%= f.text_field :date %><br />
    <%= f.label :job_name %><br />
    <%= f.text_field :job_name %><br />

    <%= f.fields_for :items do |builder| %>
        <%= builder.label :properties %><br />
        <%= builder.text_field :properties %><br />
        <%= builder.label :height %><br />
        <%= builder.text_field :height %><br />
        <%= builder.label :letter_quantity %><br />
        <%= builder.text_field :letter_quantity %><br />
        <%= builder.link_to_remove "Remove this item" %>
    <% end %>
    <%= f.link_to_add "Add a item", :items %>
<%= f.submit "Add item" %>

<% end %>

I would like to use the general jQuery ajax function to call a function that executes the query and returns the cost back to the form. Here is the basic template:

$.ajax({
  url: "test.html",
  context: document.body,
  success: function(){
    $(this).addClass("done");
  }
});

class ItemsController < ApplicationController

    def calculateCost
        #Execute SQL query here
          SELECT unit_price
          FROM CostData
          WHERE properties = form.properties
          AND height = form.height

        #Return unit_price x form.letter_quantity to form
    end

end

Most importantly, how do I setup the jQuery Ajax function to call the calculateCost function? I have been searching Google and cannot find an example of this.

Secondarily, how do a setup the query and return functionality in the calculateCost function? Any help with these questions will be greatly appreciated.

  • 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-25T02:20:32+00:00Added an answer on May 25, 2026 at 2:20 am

    include jquery, and jquery.form plugin at top of page:

    <%= javascript_include_tag "jquery-1.6.2.min.js", "jquery.form" %>
    
    
    <%= nested_form_for @estimate do |f| %>
    
        <%= f.label :date %><br />
        <%= f.text_field :date %><br />
        <%= f.label :job_name %><br />
        <%= f.text_field :job_name %><br />
    
        <%= f.fields_for :items do |builder| %>
            <%= builder.label :properties %><br />
            <%= builder.text_field :properties, :id => "properties_field" %><br />
            <%= builder.label :height %><br />
            <%= builder.text_field :height, :id => "height_field" %><br />
            <%= builder.label :letter_quantity %><br />
            <%= builder.text_field :letter_quantity, :id => "letter_quantity_field" %><br />
            <%= builder.link_to_remove "Remove this item" %>
        <% end %>
        <%= f.link_to_add "Add a item", :items %>
        <%= f.submit "Add item" %>
    
    <% end %>
    
    <script type="text/javascript">
        $("#letter_quantity_field").onBlur(function () {
            var height = $("#height_field").val();
            var properties = $("#properties_field").val();
            $.ajax({
                url: '/items/calculateCost?height=' + height + '&properties=' + properties , type: 'get', dataType: 'html',
                processData: false,
                success: function(data) {
                    if (data == "record_not_found") {
                        alert("Record not found");
                    }
                    else {
                        $("#letter_quantity_field").val(data);
                    }
                }
            });
        });
    </script>
    

    in your controller do something like.

    def calculateCost
        @cost_data = CostData.find_by_properties_and_find_by_height(params[:properties], params[:height])
        if @cost_data.blank?
          render :text => "record_not_found"
        else
          render :text => @cost_data.unit_price
        end
      end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im trying to build a form+attachment that needs to be send to email. Im
Im trying to build a form that calculates a total price based on a
I trying to build and compile my xcodeproj in command line and it is
I am trying build a DataTable one row at a time using the following
I have a std::vector< tr1::shared_ptr<mapObject> > that I'm trying build from data contained in
Trying to build the following simple example #include <boost/python.hpp> using namespace boost::python; tuple head_and_tail(object
I'm trying build a regex that will replace any characters not of the format:
Trying to build a plugin effect that will some what look better than this
Im trying to build a form which uses AJAX but its failing to execute
Trying to build a dynamic gallery that is loaded via AJAX. I'm trying to

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.