【问题标题】:Use jquery ajax in my Ruby on Rails app to refresh and text_field在我的 Ruby on Rails 应用程序中使用 jquery ajax 来刷新和 text_field
【发布时间】:2017-04-07 03:28:49
【问题描述】:

我的 ruby​​ on rails 应用程序是为一家建筑公司设计的,其中一部分我有两个模型,一个叫做 Item (rubro),另一个叫做 Budget强>。创建的每个项目都有自己的价格。在我的预算表格中,我可以选择使用嵌套表格添加不同数量的项目。在第一列中,我有一个 collection_select,它与项目 id 一起选择要添加的项目,在第二列中填写数量,在第三列中,我的想法是显示小计值(项目价格 * 数量)。

我的问题是如何获取在 collection_select 中选择的 item_id 用于获取商品价格并在小计 text_field 中显示(商品价格 * 数量)?

有可能吗?我知道我可以使用 ajax 和 jquery 来做到这一点,但我对这些很陌生。 或者有人可以给我一些其他的想法吗?

这是我的表单的一部分,我想做我所说的: _form.html.erb

 <%= f.fields_for :budget_details, :wrapper => false do |form| %>
                <tr class="fields ">
                  <td class="col-md-4">
                    <div class="col-md-9"><%=form.collection_select(:rubro_id, Rubro.all, :id, :name, {prompt: 'Seleccionar'}, class: 'form-control tooltip-required')%></div>
                    <div class="col-md-1"><%= link_to "<i class=\"fa fa-plus\"></i>".html_safe, new_rubro_path, {:class => "btn btn-sm btn-flat bg-green ", :title => "Nuevo Rubro"} %> </div></td>
                  <td class="col-md-1"> </td>
                  <td class="col-md-2"> </td>
                  <td class="col-md-1"><%=form.number_field :quantity, class: 'form-control tooltip-required'%></td>
                  <td class="col-md-2"><%=form.number_field :subtotal, class: 'form-control', :readonly => true%></td>
                  <td class="col-md-1"><%=form.number_field :utility, class: 'form-control'%> </td>
                  <td class="col-md-1"><%=form.link_to_remove '<i class="fa fa-trash"></i>'.html_safe, class: 'btn btn-danger'%></td>
                  <br>
                </tr>
            <% end %>

我把它放在我的 application.js 中(rubro = item)

$('#collection_select').change(function() {
    var rubro = this.val();
    var data = {
        'rubro_id': this.val(),
}
    $.ajax({
        type:"GET",
        url : 'rubros/rubro_price',
        data : data,
        success : function(response) {
            var quantity = $("#rubro_quantity").val();
            var price = response.price;
            $("#price").val(price);
            var subtotal_price = (quantity * price);
            $("#subtotal_price").val(subtotal_price);
        },
        error: function() {
            alert('Error');
        }
    });
});

还有我的 rubro_controller.rb

 def rubros_price
    @rubro = Rubro.find(params[:data])
  end

【问题讨论】:

  • 在什么情况下“获取”它?客户端?服务器端?
  • 请同时粘贴一些代码。具体来说,您创建表单的内容和方式。另外,到目前为止,您在 jquery/javascript 中尝试过什么?

标签: javascript jquery ruby-on-rails ruby ajax


【解决方案1】:

我在这里写伪代码,如果你在这里提供你的代码,那么我可以帮助你。

如果您需要更清楚,请阅读下面的每一行并输入您的 cmets。

Select your item from the select box as you said from "collection_select".
 Call the ajax for item price which you selected or you can take from your page if price presents anywhere.
  You will get the price in ajax response.
 Now you have "Item", "quantity" and "price"(from ajax response).
You can calculate subtotal (price * quantity) and fill text_box.

根据您的反馈,我将 ajax 代码按照上面的伪:

$('#collection_select').change(function() {
  var item = this.val();
  var data = {
    'item_id': this.val();
    }
  $.ajax({
    type:"GET",
    url : "/item_price", /*Here in controller you can find price as per item id*/
    data : data,
    success : function(response) {
       var quantity = $("#item_quantity").val();
       var price = response.price;
       var subtotal_price = (quantity * price);
       $("#subtotal_price").val(subtotal_price);
    },
    error: function() {
        alert('Error occured');
    }
 });
});

【讨论】:

  • 感谢您回答@umishra。你能更具体地回答你的问题吗?你能给我看一个更具体的 ajax 伪代码吗?
  • 嘿,@EmilioCenturión 我已根据您的要求放置了 ajax 代码。但我不确定这是否真的有效,因为我只写了假设。请让我知道你还需要什么。如果你愿意分享你的代码库,那么我可以更好或更准确地帮助你。
  • @EmilioCenturión 您的问题与计算小计价格有关,这是我为您写的。现在,如果您想获得更多答案,那么您必须提出一个新问题。有意义吗?
  • 感谢@umishra 的帮助
猜你喜欢
  • 1970-01-01
  • 2013-05-18
  • 2011-04-15
  • 1970-01-01
  • 1970-01-01
  • 2014-11-05
  • 1970-01-01
  • 2011-08-20
  • 2020-09-20
相关资源
最近更新 更多