如何使用 remote_function 使用 Rails 3 进行 Ajax 调用? [英] How to make Ajax calls with Rails 3 using remote_function?

查看:16
本文介绍了如何使用 remote_function 使用 Rails 3 进行 Ajax 调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在选择框上进行 onchange 调用.对于我的应用程序,我使用 jquery.js 和 rails.js 如上所述来利用 UJS.但是生成的代码仍然是用于 Prototype 而不是用于 jquery ajax 调用.我的代码如下所示:

I am trying to make an onchange call on a select box.For my application i am using jquery.js and rails.js as mentioned to make use of UJS. But still the code that is generated is for Prototype and not for jquery ajax calls. My code looks like the following:

<%= select_tag :category, 
       options_for_select(
           Category.find(:all,:select=>"name,id").collect{|c| [c.name,c.id]}),
       :onchange => remote_function(:url => {:controller => "posts", 
                                             :action => "filter_post",
                                             :filter =>"category"}, 
                                    :with=>"'category_id=' + $('#category').val()") %>

由此产生的代码是:

new Ajax.Request('/posts/filter_posts?filter=category', {asynchronous:true, evalScripts:true,
parameters:'category_id=' + $('#category').val() + '&authenticity_token=' +
encodeURIComponent('CCyYj1wqXtddK6pUV6bAxw0CqZ4lbBxDGQHp13Y/jMY=')})

推荐答案

正如 Sergei 所说:rails 3 中没有 remote_function.

As Sergei said: There is no remote_function in rails 3.

解决方案是使用以下内容不显眼地添加 onchange 处理程序:

The solution is to unobtrusively add the onchange handler using something like:

$(function($) {
    $("#selector_for_element").change(function() {
        $.ajax({url: '<%= url_for :action => 'action_name', :id => @model.id %>',
        data: 'selected=' + this.value,
        dataType: 'script'})
    });
});

这将调用当前控制器中的 action_name 方法,将 selected=... 的结果作为参数传递.现在您可以返回一段 javascript,例如使用以下内容:

This will call the action_name method in the current controller, passing the result of selected=... as a parameter. Now you can return a piece of javascript, for instance using something like:

calculated = calculate_some_value_using(params[:selected])
render(:update) {|page|
  page << "$('#selector_for_field_to_update).html(#{calculated})"
}

在控制器中.您附加到''page'' 的字符串应该是有效的javascript,如果您使用$.ajax,它将自动执行.

in the controller. The string you append to ''page'' should be valid javascript and will be executed automatically if you use $.ajax.

仅供参考,我们最近为 jquery-ujs 添加了一个选择更改帮助程序,因此您现在可以使用内置的 jquery-ujs 适配器执行这种不显眼的方式:

FYI, we recently added a select change helper to jquery-ujs, so you can do this unobtrusive way using the built-in jquery-ujs adapter now:

<%= select_tag :category_id, 
   options_for_select(
       Category.find(:all,:select=>"name,id").collect{|c| [c.name,c.id]}),
   :data => { :remote => true, :url => url_for(:controller => "posts", 
                                         :action => "filter_post",
                                         :filter =>"category") } %>

尽管公平地说,添加选择更改事件确实永远不会不引人注目",因为禁用 javascript 的用户没有优雅的回退,除非您的表单不依赖于 AJAX 请求的结果.

Though to be fair, adding a select change event really isn't ever going to be "unobtrusive" as there is no graceful fallback for users with javascript disabled unless your form doesn't depend on the result of the AJAX request.

这篇关于如何使用 remote_function 使用 Rails 3 进行 Ajax 调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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