简体   繁体   English

typeahead.js和rails搜索,远程选项不显示结果

[英]typeahead.js and rails search, remote option not displaying results

I'm trying to complete an autocomplete searchbar (for titles and description of photos) on a project. 我正在尝试在项目上完成自动完成搜索栏(用于标题和照片说明)。 My partner on this project has implemented the following search action in the Photos controller 我在该项目上的合作伙伴已在“照片”控制器中实施了以下搜索操作

def search_results
  tag =  params[:q].values.first
  puts(tag)
  @photos= @q.result(distinct: true).to_a
  @photos += Photo.tagged_with(tag).flatten
  @photos.uniq!
    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @photos }
    end 
end

//And in my application.js
$(document).ready(function(){
 $('#typeaheadBar').typeahead([
   {
     name: 'mysearch',
     displayKey: 'title',
     remote: '/search?utf8=%E2%9C%93&q%5Btitle_or_caption_cont%5D=%QUERY'
   }
 ]);

});

I can't see what's wrong, but my instructor says I'm close, I know typeahead is returning something, but I don't know how to reference it. 我看不到出了什么问题,但是我的教练说我很接近,我知道打字提前返回了一些东西,但是我不知道如何引用它。 Could someone point me in the right direction or point out what I'm doing wrong? 有人可以指出我正确的方向还是指出我做错了什么? Thank you. 谢谢。

typeahead does not have remote option in the dataset definitions. typeahead在数据集定义中没有remote选项。 See the available options here . 此处查看可用选项。 You should use the Bloodhound suggestion engine to fetch your data from remote. 您应该使用Bloodhound建议引擎从远程获取数据。 You can find examples here . 您可以在此处找到示例。 In your case: 在您的情况下:

var engine = new Bloodhound({
  datumTokenizer: Bloodhound.tokenizers.obj.whitespace('title'),
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  remote: '../data/films/queries/%QUERY.json'
});

engine.initialize();

// The first argument is an option object, the additional arguments are 
// the definitions of your datasets.
$('#typeaheadBar').typeahead(null, {
  name: 'mysearch',
  displayKey: 'title',
  source: engine.ttAdapter()
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM