在 Rails 中,如何为 Rails 设置 Twitter Typeahead.js gem?

In Rails, how do I set up the Twitter Typeahead.js gem for Rails?

bootstrap-typeahead-rails gem's README kicks the question over to Twitter's typeahead.js README。这还有很多不足之处。

这个 provides detailed instructions for the twitter-typeahead-rails gem。我想为 bootstrap-typeahead-rails gem.

看到类似的内容

这是我的向导。它遵循 。此示例假定一个模型 Thing 并向索引视图添加一个表单,用于通过模型的 name 属性搜索 things

一些注意事项:

  • 我正在使用 Rails 4 (4.2.1)。
  • 对于搜索查询,我使用 Searchlight gem
  • 对于模板,我使用 slim-rails gem
  • 样式留给开发人员练习。

将 gem 添加到 gem 文件

# Gemfile

# Typeahead gem
gem 'bootstrap-typeahead-rails'

# Optional gems
gem 'searchlight'
gem 'slim-rails'

在资产清单中包含预输入文件

样式表(SASS)

# app/assets/stylesheets/application.scss

  *= require bootstrap-typeahead-rails

Javascript

# app/assets/javascripts/application.js

//= require bootstrap-typeahead-rails
//= require_tree .

将预先输入的路线添加到路线文件

# config/routes.rb

  get 'things/typeahead/:query' => 'things#typeahead'

添加提前输入javascript代码

# app/assets/javascripts/things.js

var onReady = function() {

  // initialize bloodhound engine
  var searchSelector = 'input.typeahead';

  var bloodhound = new Bloodhound({
    datumTokenizer: function (d) {
      return Bloodhound.tokenizers.whitespace(d.value);
    },
    queryTokenizer: Bloodhound.tokenizers.whitespace,

    // sends ajax request to remote url where %QUERY is user input
    remote: '/things/typeahead/%QUERY',
    limit: 50
  });
  bloodhound.initialize();

  // initialize typeahead widget and hook it up to bloodhound engine
  // #typeahead is just a text input
  $(searchSelector).typeahead(null, {
    displayKey: 'name',
    source: bloodhound.ttAdapter()
  });

  // this is the event that is fired when a user clicks on a suggestion
  $(searchSelector).bind('typeahead:selected', function(event, datum, name) {
    //console.debug('Suggestion clicked:', event, datum, name);
    window.location.href = '/things/' + datum.id;
  });
};

将相关 methods/actions 添加到控制器

# app/controllers/things_controller.rb

  # GET /things
  # GET /things.json
  def index
    @search = ThingSearch.new(search_params)
    @things = search_params.present? ? @search.results : Thing.all
  end

  # GET /things/typeahead/:query
  def typeahead
    @search  = ThingSearch.new(typeahead: params[:query])
    render json: @search.results
  end

  private

  def search_params
    params[:thing_search] || {}
  end

将搜索表单添加到索引视图(使用 SLIM gem)

# app/views/things/index.html.slim

div.search.things
  = form_for @search, url: things_path, method: :get do |f|
    div.form-group.row
      div.col-sm-3
      div.col-sm-6
        = f.text_field :name_like, {class: 'typeahead form-control',
            placeholder: "Search by name"}
        = f.submit 'Search', {class: 'btn btn-primary'}
      div.col-sm-3.count
        | Showing <strong>#{@things.length}</strong> Thing#{@things.length != 1 ? 's' : ''}

创建 Searchlight 搜索 class

如果您不想使用 Searchlight,请使用模型中的 ActiveRecord 查询接口。

# app/searches/thing_search.rb

class ThingSearch < Searchlight::Search
  search_on Thing.all

  searches :name_like, :typeahead

  # Note: these two methods are identical but they could reasonably differ.
  def search_name_like
    search.where("name ILIKE ?", "%#{name_like}%")
  end

  def search_typeahead
    search.where("name ILIKE ?", "%#{typeahead}%")
  end
end

@klenwell 的回答已经过时了。这是我如何让它工作的:

我正在使用:

  • Bootstrap v3.3.6
  • 猎犬 0.11.1
  • bootstrap3-typeahead 3.1.0
  • jQuery 2.2.0

我的模型叫做Destination

app/models/destination_search.rb:

class DestinationSearch < Searchlight::Search

  def base_query
    Destination.all
  end

  def search_typeahead
    query.where("name ILIKE", "%#{typeahead}%")
  end

end

控制器:

class DestinationsController < APIController

  def typeahead
    render json: DestinationSearch.new(typeahead: params[:query]).results
  end

end

JS:

var bloodhound = new Bloodhound({
  datumTokenizer: function (d) {
    return Bloodhound.tokenizers.whitespace(d.value);
  },
  queryTokenizer: Bloodhound.tokenizers.whitespace,

  remote: {
    url: '/api/destinations/typeahead?query=%QUERY',
    wildcard: "%QUERY",
  },
  limit: 10
});
bloodhound.initialize();

$(document).ready(function () {
  $(".destination-typeahead").typeahead({
    source: function (query, process) {
      return bloodhound.search(query, process, process);
    },
  });
});

并且在视图中:

<%= text_field_tag :destination, class: "destination-typeahead" %>

我将 process 方法两次传递给 bloodhound.search 感觉有点老套 - 这是因为 bloodhound#search takes two callbacks as arguments,一个处理 cached/prefetched 数据和处理通过 AJAX 动态提取的数据的数据。我可能没有 100% 正确地使用#search,但这种方法有效,而且是一个简单的开始。