2011-08-26 72 views
2

我正在尝试构建支持多个订单项的估算表单。在以下字段填写完成的估计行项目后:使用jQuery查询数据库Ajax in Rails 3

  • 性能
  • 高度
  • letter_quantity

我想查询数据库表中获得单价由乘letter_quantity并显示订单项的费用。我的想法是执行查询并在letter_quantity字段中显示订单项的成本onBlur。

这里是我的形式:

<%= 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 %> 

我想用一般的jQuery AJAX函数来调用执行查询和返回的费用回形式的函数。这里是基本的模板:

$.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 

最重要的是,我如何设置jQuery的Ajax函数来调用calculateCost函数?我一直在搜索谷歌,找不到这样的例子。

其次,如何设置calculateCost函数中的查询和返回功能?任何有关这些问题的帮助将不胜感激。

回答

3

包括jQuery和jquery.form插件在页面顶部:

<%= 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> 

在你的控制器做这样的事情。

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

这是行不通的。我按照您的指定设置了ajax函数,但在Google开发工具中查看请求时,我收到了404 Not Found状态/响应。 –

+1

我添加了一个Collection路由来映射calculateCost操作,现在它可以工作。谢谢你的帮助! –