错误未定义的方法`remote_function“(如何让Ajax和Rails 3的使用remote_function调用) [英] error undefined method `remote_function' (how to make Ajax calls with Rails 3 using remote_function)

查看:201
本文介绍了错误未定义的方法`remote_function“(如何让Ajax和Rails 3的使用remote_function调用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现这个功能(remote_function)为原型的辅助,将其从使用Rails 3.1,以及如何更改AJAX?框架中删除的部分。

 国家
      %选择#国{:名称=> 国家,:的onchange => remote_function(:更新=>region_city',:URL => {:动作=>:get_regions_cities_for_country}:与=>Form.Element.serialize(本))}
        = options_for_select(Country.find(:所有).MAP {| U | [u.name,u.id]})
      %的div {:ID => region_city}
        地区
        %选择#区{:NAME => 区域:的onchange => remote_function(:更新=>城市,:URL => {:动作=>:get_cities}:与=>Form.Element.serialize(本))}
          = options_for_select(Country.find(:第一).regions.find(:所有).MAP {| U | [u.name,u.id]})
        市
        #{select_tag(:城市,options_for_select(Country.find(:第一).regions.find(:第一).cities.find(:所有).MAP {| U | [u.name,u.id]})) }
 

解决方案

观点:

  = select_tag(:区,<期权价值='0'>#{T('请选择地区')}< /选项>中html_safe + options_from_collection_for_select (District.all,ID,标题),:数据远程'=>真,:数据链接'=> url_for(:控制器=>'选择',:动作= >'的getData),:数据型=>JSON)
= select_tag(:国家<期权价值='0'>#{T('请选择国家')}< /选项>中html_safe,:数据远程'=>真, :数据链接'=> url_for(:控制器=>'选择',:动作=>'的getData),:数据型=>JSON)
= select_tag(:城市,<期权价值='0'>#{T('请选择城市')}< /选项>中html_safe)

:JavaScript的

        $(文件)。就绪(函数(){
          $('#区)生活(AJAX:成功',功能(EVT,数据,身份,XHR){
            VAR selectbox2 = $('#国家);
            selectbox2.empty();
            VAR OPT = $('<选项/>');
            opt.attr('值',0);
            opt.text(#{T('请选择国家')});
            opt.appendTo(selectbox2);
            $每个(数据,函数(指数值){
              VAR OPT = $('<选项/>');
              opt.attr('值',值[0]);
              opt.text(值[1]);
              opt.appendTo(selectbox2);
            });
            VAR selectbox3 = $('#城市);
            selectbox3.empty();
            VAR OPT = $('<选项/>');
            opt.attr('值',0);
            opt.text(#{T('请选择城市')});
            opt.appendTo(selectbox3);
          });
          $('#州)生活(AJAX:成功',功能(EVT,数据,身份,XHR){
            VAR selectbox3 = $('#城市);
            selectbox3.empty();
            $每个(数据,函数(指数值){
              VAR OPT = $('<选项/>');
              opt.attr('值',值[0]);
              opt.text(值[1]);
              opt.appendTo(selectbox3);
            });
          });
        });
 

控制器:

 类SelectController<的ApplicationController

  高清的getData

    @ data_for_select1 = PARAMS [:地区]
    如果@ data_for_select1
      @ data_for_select2 = State.where(:district_id => @ data_for_select1)。所有
      渲染:JSON => @ data_for_select2.map {| C | [c.id,c.title]}
    其他
      @ data_for_select1 = PARAMS [:状态]
      @ data_for_select2 = City.wh​​ere(:STATE_ID => @ data_for_select1)。所有
      渲染:JSON => @ data_for_select2.map {| C | [c.id,c.name]}
    结束
  结束
结束
 

路线:

 获得的资产'=> 选择#的getData
 

请注意:你必须有一个本地化的短语,否则JS不能正常工作

I found that this function (remote_function) was part of the Prototype helper which was removed from the framework with Rails 3.1 and how to change on ajax?

      country
      %select#country{:name => "country", :onchange => remote_function(:update => 'region_city', :url => { :action => :get_regions_cities_for_country}, :with => 'Form.Element.serialize(this)')}
        = options_for_select( Country.find(:all).map {|u| [u.name,u.id]})
      %div{:id => "region_city"}
        region
        %select#region{:name => "region", :onchange => remote_function(:update => 'city', :url => { :action => :get_cities}, :with => 'Form.Element.serialize(this)')}
          = options_for_select(Country.find(:first).regions.find(:all).map {|u| [u.name,u.id]})
        city
        #{select_tag(:city, options_for_select( Country.find(:first).regions.find(:first).cities.find(:all).map {|u| [u.name,u.id]} ))}

解决方案

views:

= select_tag(:district,"<option value='0'>#{t('Please select district')}</option>".html_safe+options_from_collection_for_select(District.all, "id", "title"),:'data-remote' => 'true',:'data-url' => url_for(:controller => 'select', :action => 'getdata'),:'data-type' => 'json')
= select_tag(:state,"<option value='0'>#{t('Please select state')}</option>".html_safe,:'data-remote' => 'true',:'data-url' => url_for(:controller => 'select', :action => 'getdata'),:'data-type' => 'json')
= select_tag(:city,"<option value='0'>#{t('Please select city')}</option>".html_safe)

:javascript

        $(document).ready(function() {
          $('#district').live('ajax:success', function(evt, data, status, xhr) {
            var selectbox2 = $('#state');
            selectbox2.empty();
            var opt = $('<option/>');
            opt.attr('value', "0");
            opt.text("#{t('Please select state')}");
            opt.appendTo(selectbox2);
            $.each(data, function(index, value) {
              var opt = $('<option/>');
              opt.attr('value', value[0]);
              opt.text(value[1]);
              opt.appendTo(selectbox2);
            });
            var selectbox3 = $('#city');
            selectbox3.empty();
            var opt = $('<option/>');
            opt.attr('value', "0");
            opt.text("#{t('Please select city')}");
            opt.appendTo(selectbox3);
          });
          $('#state').live('ajax:success', function(evt, data, status, xhr) {
            var selectbox3 = $('#city');
            selectbox3.empty();
            $.each(data, function(index, value) {
              var opt = $('<option/>');
              opt.attr('value', value[0]);
              opt.text(value[1]);
              opt.appendTo(selectbox3);
            });
          });
        });

controller:

class SelectController < ApplicationController

  def getdata

    @data_for_select1 = params[:district]
    if @data_for_select1
      @data_for_select2 = State.where(:district_id => @data_for_select1).all
      render :json => @data_for_select2.map{|c| [c.id, c.title]}
    else
      @data_for_select1 = params[:state]
      @data_for_select2 = City.where(:state_id => @data_for_select1).all
      render :json => @data_for_select2.map{|c| [c.id, c.name]}
    end
  end
end

routes:

  get 'assets' => 'select#getdata'

note: you must have a localization used phrases, otherwise js does not work

这篇关于错误未定义的方法`remote_function“(如何让Ajax和Rails 3的使用remote_function调用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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