Ruby on Rails - 使用AJAX获取数据的jQuery [英] Ruby on Rails - jQuery to get data using AJAX

查看:129
本文介绍了Ruby on Rails - 使用AJAX获取数据的jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下型号:

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

当用户在表单上选择成型时,我想通过ajax获得成型成本并使用它来使用javascript生成报价。以下是表单中的选择标记:

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

以下是javascript文件的一部分:

Here is part of the javascript file:

$(document).ready(function () {

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

});

如何使用Ajax设置变量moulding_1_price?

How do I use Ajax to set the variable moulding_1_price?

推荐答案

如果您想通过ajax在rails控制器中执行此操作,您只需将每个选项的值设置为您正在查找的模具的ID,并且使用jQuery的ajax方法将其发送到您的控制器:

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:

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


        $.ajax({
          url: theURL

        });

然后确保在routes.rb文件中设置路线:

Then make sure you have a route set in your routes.rb file:

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

在你的yourMouldingsController中,定义一个自定义方法:

And in your yourMouldingsController, define a custom method:

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

end

默认情况下,这将呈现ajaxMouldings.js.erb。因此,在您的视图中,请确保您有一个具有该名称的文件。这是嵌入式JavaScript,因此您可以使用它来替换您希望显示该信息的页面上的某个div:

By default, this will render 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:

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

这篇关于Ruby on Rails - 使用AJAX获取数据的jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆