Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.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 Rails在多个类别上自动完成_Ruby On Rails_Jquery Ui_Autocomplete - Fatal编程技术网

Ruby on rails Rails在多个类别上自动完成

Ruby on rails Rails在多个类别上自动完成,ruby-on-rails,jquery-ui,autocomplete,Ruby On Rails,Jquery Ui,Autocomplete,下面我将使用jqueryui中的autocomplete在搜索框中实现自动完成。然而,我也希望能够包括每个搜索建议所属的类别,并对建议进行分组,类似于潘多拉如何在歌曲/艺术家/专辑上进行匹配 我的模型如下所示: class SearchSuggestion < ActiveRecord::Base attr_accessible :popularity, :term, :type def self.terms_for(prefix) suggestions = wher

下面我将使用jqueryui中的autocomplete在搜索框中实现自动完成。然而,我也希望能够包括每个搜索建议所属的类别,并对建议进行分组,类似于潘多拉如何在歌曲/艺术家/专辑上进行匹配

我的模型如下所示:

class SearchSuggestion < ActiveRecord::Base
  attr_accessible :popularity, :term, :type

  def self.terms_for(prefix)
     suggestions = where("term like ?", "#{prefix}_%")
     suggestions.order("popularity desc").limit(10).pluck(:term)
  end
end
class SearchSuggestionsController < ApplicationController
  def index
    render json: SearchSuggestion.terms_for(params[:term])
  end
end
def self.terms_for(prefix)
   suggestions = where("term like ?", "#{prefix}_%").order("popularity desc").limit(10)
   suggestions.collect {|suggestion| {"value" => suggestion["term"], "type" => suggestion["suggestion_type"] }}
end
classsearchsuggestion
我的控制器是这样的:

class SearchSuggestion < ActiveRecord::Base
  attr_accessible :popularity, :term, :type

  def self.terms_for(prefix)
     suggestions = where("term like ?", "#{prefix}_%")
     suggestions.order("popularity desc").limit(10).pluck(:term)
  end
end
class SearchSuggestionsController < ApplicationController
  def index
    render json: SearchSuggestion.terms_for(params[:term])
  end
end
def self.terms_for(prefix)
   suggestions = where("term like ?", "#{prefix}_%").order("popularity desc").limit(10)
   suggestions.collect {|suggestion| {"value" => suggestion["term"], "type" => suggestion["suggestion_type"] }}
end
class SearchSuggestionController

我如何修改术语以便可以包括:以autocomplete可以处理的格式键入json,我将如何呈现它?

我找到了一种方法来做我想做的事情,所以我想我会回答我自己的问题。我发现了handy collect方法,并编写了如下函数:

class SearchSuggestion < ActiveRecord::Base
  attr_accessible :popularity, :term, :type

  def self.terms_for(prefix)
     suggestions = where("term like ?", "#{prefix}_%")
     suggestions.order("popularity desc").limit(10).pluck(:term)
  end
end
class SearchSuggestionsController < ApplicationController
  def index
    render json: SearchSuggestion.terms_for(params[:term])
  end
end
def self.terms_for(prefix)
   suggestions = where("term like ?", "#{prefix}_%").order("popularity desc").limit(10)
   suggestions.collect {|suggestion| {"value" => suggestion["term"], "type" => suggestion["suggestion_type"] }}
end