Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails jqueryui与RoR的问题_Ruby On Rails_Ruby On Rails 3_Jquery Ui_Autocomplete - Fatal编程技术网

Ruby on rails jqueryui与RoR的问题

Ruby on rails jqueryui与RoR的问题,ruby-on-rails,ruby-on-rails-3,jquery-ui,autocomplete,Ruby On Rails,Ruby On Rails 3,Jquery Ui,Autocomplete,如何从Project.all和Project.name等模型中获取所有项目的列表,并将其移动到javascript中列出的列表中,然后使用Jquery UI自动完成打印出来 我想你可能正在寻找这样的东西: def project_list list=Project.all.map{|i|[i.name,i.id]} arr= [["No project",0]].concat(list.sort{|a,b| a[0]<=>b[0]}).to_json rende

如何从Project.all和Project.name等模型中获取所有项目的列表,并将其移动到javascript中列出的列表中,然后使用Jquery UI自动完成打印出来


我想你可能正在寻找这样的东西:

 def project_list  
   list=Project.all.map{|i|[i.name,i.id]}
  arr= [["No project",0]].concat(list.sort{|a,b| a[0]<=>b[0]}).to_json
  render :json =>arr
 end
这里有一个相对完整的示例,其中一些生成了机场的自动完成列表

   $("#airport, #station").autocomplete({
        delay: 1000,
        search: function(event,ui){
            tag = event.target.id
        },
        source: function(request, response){
            //pass request to server
            $.ajax({
                url: "/tags/"+ tag +"/list",
                dataType: "json",
                data: {
                    style: "full",
                    maxRows: 12,
                    term: request.term
                },
                success: function( data ) {
                    response( $.map( data, function( item ) {
                        if (tag == "airport"){
                            return {
                                label: item.airport.fd_name
                            }
                        }
                        else
                        {
                            return {
                                label: item.station.fd_name
                            }

                        }

                    }));
                }
            });
        },
        minLength: 2,
        open:   function( event, ui ) {
            stripe($(this));
        },
        close: function() {
            $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
        }
    });
以下是相关的控制器代码:

def list
    @tags= params[:tagclass].titleize.constantize.where("fd_name ilike ? ",params[:term]+"%").order('fd_name')
    render :json =>  @tags.to_json(:only=>[:fd_code, :fd_name]), :layout=>false
  end

`

此时javascript正在查看var availableTags=[projects,here]project\u list在此处输出一个数组-更新后输出json。有很多方法可以做到这一点——我的代码中没有一个很好的简单示例,我正在剪切、粘贴和整理一些片段以尝试澄清,但可能会造成混乱……是否应该有一个{在JS im结束时收到一个错误是-代码不完整-成功函数需要对从服务器返回的数据进行处理…
$(“#标记”).autocomplete({source:function(request,response){//pass request to server$.ajax({url:“/projects/project\u list”),数据类型:“json”,数据:{style:“full”,最大行数:12,术语:request.term},最小长度:1,选择:函数(事件,ui){log(ui.item?ui.item.value:+this.value);}}});
   $("#airport, #station").autocomplete({
        delay: 1000,
        search: function(event,ui){
            tag = event.target.id
        },
        source: function(request, response){
            //pass request to server
            $.ajax({
                url: "/tags/"+ tag +"/list",
                dataType: "json",
                data: {
                    style: "full",
                    maxRows: 12,
                    term: request.term
                },
                success: function( data ) {
                    response( $.map( data, function( item ) {
                        if (tag == "airport"){
                            return {
                                label: item.airport.fd_name
                            }
                        }
                        else
                        {
                            return {
                                label: item.station.fd_name
                            }

                        }

                    }));
                }
            });
        },
        minLength: 2,
        open:   function( event, ui ) {
            stripe($(this));
        },
        close: function() {
            $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
        }
    });
def list
    @tags= params[:tagclass].titleize.constantize.where("fd_name ilike ? ",params[:term]+"%").order('fd_name')
    render :json =>  @tags.to_json(:only=>[:fd_code, :fd_name]), :layout=>false
  end