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

查看:69
本文介绍了如何使用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})"
}

控制器中的

.附加到页面"的字符串应为有效的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天全站免登陆