简体   繁体   English

Ruby on Rails-jQuery使用AJAX获取数据

[英]Ruby on Rails - jQuery to get data using AJAX

I have the following model: 我有以下模型:

create_table "mouldings", :force => true do |t|
  t.string   "suppliers_code"
  t.datetime "created_at"
  t.datetime "updated_at"
  t.string   "name"
  t.integer  "supplier_id"
  t.decimal  "length",         :precision => 3, :scale => 2
  t.decimal  "cost",           :precision => 4, :scale => 2
  t.integer  "width"
  t.integer  "depth"
end

When a user selects a moulding on a form I want to get the cost of the moulding by ajax and use it to get a generate a quote using javascript. 当用户在表单上选择成型时,我想通过ajax获取成型的成本,并使用它来获取使用JavaScript生成的报价。 Here is the select tag from the form: 这是表单中的选择标签:

<select id="order_item_moulding_1_id" name="order_item_moulding_1[id]">
  <option value="">Select a moulding 1</option> 
  <option value="1">123 589 698</option> 
  <option value="2">897 896 887</option> 
  <option value="3">876 234 567</option>
</select>

Here is part of the javascript file: 这是javascript文件的一部分:

$(document).ready(function () {

  $('#order_item_moulding_1_id').change(function () {
    var moulding_1_price = ;
  });

});

How do I use Ajax to set the variable moulding_1_price? 如何使用Ajax设置变量moulding_1_price?

If you wanted to do it in your rails controller via ajax, you'd just set the value of each option to the ID of the molding you were looking up, and send that to your controller with jQuery's ajax method: 如果要通过ajax在rails控制器中执行此操作,只需将每个选项的值设置为要查找的成型ID,然后使用jQuery的ajax方法将其发送到控制器:

 var theVal = $('#order_item_moulding_1_id').val();
 var theURL = '/someUniqueRoutingKeywordSuchAsMouldingAjax/' + theVal;


        $.ajax({
          url: theURL

        });

Then make sure you have a route set in your routes.rb file: 然后,确保在您的routes.rb文件中设置了一条路由:

 match 'someUniqueRoutingKeywordSuchAsMouldingAjax/:id', :to => 'yourMouldingsController#ajaxMouldings'

And in your yourMouldingsController, define a custom method: 并在yourMouldingsController中定义一个自定义方法:

def ajaxMouldings
@moulding = Moulding.find(params[:id])

end

By default, this will render ajaxMouldings.js.erb. 默认情况下,这将呈现ajaxMouldings.js.erb。 So in your views, make sure you have a file with that name. 因此,在您的视图中,请确保您具有该名称的文件。 That's embedded javascript, so you can use it to replace some div on your page where you want that information to appear: 那是嵌入式javascript,因此您可以使用它来替换页面上要显示该信息的div:

// in ajaxMouldings.js.erb
// be sure to escape any dynamic values!
 alert('Hey, it worked!');

var theHTML = '<div class="moulding_title"><%= escape_javascript(@moulding.title) %></div><div class="moulding info"><%= escape_javascript(@moulding.info) %></div>';

$('#someUniqueDiv').html(theHTML);

Obviously, you'll want to throw in a few safeguards against bad data, etc... but that should get you on the right track. 显然,您将需要采取一些防范措施来防止不良数据等……但是,这应该可以使您走上正确的道路。

You could do an ajax call to retrieve the data, or you could use HTML5 data attribute. 您可以执行ajax调用来检索数据,也可以使用HTML5数据属性。

For the second solution, you would add data-price attribute to your options tags, so it would look like this: 对于第二种解决方案,您可以将data-price属性添加到options标签,因此如下所示:

<option value="1" data-price="123">123 589 698</option> 
<option value="2" data-price="456">897 896 887</option> 
<option value="3" data-price="789">876 234 567</option>

Then, in your JS file: 然后,在您的JS文件中:

$(document).ready(function () {

  $('#order_item_moulding_1_id').change(function () {
    var moulding_1_price = $(this).find('option:selected').attr('data-price');
  });

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM