如何使用jQuery的AJAX请求和Ruby on Rails的“渲染”的方法togheter? [英] How to use the jQuery AJAX request and the Ruby on Rails 'render' method togheter?

查看:156
本文介绍了如何使用jQuery的AJAX请求和Ruby on Rails的“渲染”的方法togheter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Ruby on Rails 3.0.7和jQuery 1.6.1,我想知道如何处理响应 XHR 状态 AJAX请求后变量。

Ajax请求是:

  $ jQuery.ajax({
  键入:POST,
  网址:< request_to_the_controller_method>中,//请参见下面的code
  错误:函数(XHR,状态,前){
    //处理与XHR','地位'和'前'变量
  }
  成功:函数(jqXHR,状态,前){
    //处理与XHR','地位'和'前'变量
  }
});
 

在控制器我有:

 的respond_to办|格式|
  format.js {
    使...#这里应适当规定的Ruby on Rails的渲染的方法
    :状态=> 200
   }
结束
 

如何应在控制器中的渲染方法来响应AJAX请求,这样就可以处理响应值来表示( XHR 状态变量)在错误成功 AJAX请求的部分?那么,我该怎么处理响应值后,我取出那些(例如,以显示一条警告消息,这些价值)?

在一些世界,我想要做的是(控制器),以一个Ajax请求一些数据,然后(在另一侧)响应处理此数据。

解决方案

请参阅

  

<一个href="http://stackoverflow.com/questions/366311/how-do-you-handle-rails-flash-with-ajax-requests">How你怎样对待铁路的闪光灯Ajax请求?

  

错误Rails中处理与阿贾克斯3

以及了解详细信息。这里是的优秀文章在轨道上的话题, Rails的jQuery的UJS驱动程序,jQuery和rails.js

  
      
  1. 在它找到远程链接,表单和输入,并覆盖它们的点击事件,通过AJAX提交给服务器。
  2.   
  3. 在它触发6 JavaScript的事件,你可以绑定回调使用和处理Ajax响应。
  4.   

然后本文走过如何做到这一切使用jQuery,UJS和护栏。

I am using Ruby on Rails 3.0.7 and jQuery 1.6.1 and I would like to know how to handle response xhr, status and ex variables after an AJAX request.

The AJAX request is:

$jQuery.ajax({
  type:    "POST",
  url:     "<request_to_the_controller_method>", // see the code below
  error: function(xhr, status, ex) {
    // handling with 'xhr', 'status' and 'ex' variables
  }
  success: function(jqXHR, status, ex) {
    // handling with 'xhr', 'status' and 'ex' variables
  }
});

In the controller I have:

respond_to do |format|
  format.js { 
    render ... # here should be properly stated the Ruby on Rails 'render' method 
    :status => 200 
   }
end

How SHOULD be stated in the controller the render method to respond to the AJAX request so that it is possible to handle response values (xhr, status and ex variables) in the error or success section of the AJAX request? Then, how can I handle the response values after I retrieved those (for example to show those values in an alert message)?

In few worlds, what I would like to do is (in the controller) to respond to an AJAX request with some data and then (on the other side) handle this data.

解决方案

See

How do you handle Rail's flash with Ajax requests?

and

Error Handling with Ajax in Rails 3

as well for details. Here's an excellent article on the topic of rails, the Rails jQuery UJS driver, jQuery, and rails.js

  1. It finds remote links, forms, and inputs, and overrides their click events to submit to the server via AJAX.
  2. It triggers six javascript events to which you can bind callbacks to work with and manipulate the AJAX response.

And then the article walks through how to do all this using jQuery, UJS and rails.

这篇关于如何使用jQuery的AJAX请求和Ruby on Rails的“渲染”的方法togheter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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