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函数中的查询和返回功能?任何有关这些问题的帮助将不胜感激。
这是行不通的。我按照您的指定设置了ajax函数,但在Google开发工具中查看请求时,我收到了404 Not Found状态/响应。 –
我添加了一个Collection路由来映射calculateCost操作,现在它可以工作。谢谢你的帮助! –