Typeahead不向服务器发送请求

时间:2014-03-08 18:16:01

标签: ruby-on-rails typeahead.js

我遵循了本教程 https://shellycloud.com/blog/2013/10/adding-search-and-autocomplete-to-a-rails-app-with-elasticsearch

当我输入文本字段时,我没有看到任何ajax请求被发送到服务器。 (我将typeahead.js添加到我的资产中)

我缺少什么?

这是我的代码

视图:

.col-xs-12.col-sm-12.col-md-12.col-lg-6.col-lg-offset-3
  %form#search_form{action: "/drugs/search", method: "get", text: "search", role:"search"}
    .input-group
      %input.form-control{type: "text", placeholder: "Search", name: "query", id: "drug_search", autocomplete: "off"}
      %span.input-group-btn
        %button.btn.btn-default{type: "submit"} Search
  .panel-group#drug-list
    = render @drugs
    = will_paginate @drugs, renderer: BootstrapPagination::Rails

控制器:

  def autocomplete
    render json: Drug.search(params[:query], autocomplete: true, limit: 6).map(&:generic_name)
  end

JS

$ ->
  $("#drug_search").typeahead
    name: "drug"
    remote: "/drugs/autocomplete?query=%QUERY"

1 个答案:

答案 0 :(得分:0)

发布v0.10.0时,API发生了轻微变化。试试这个:

var drugSource = new Bloodhound({
  datumTokenizer: function(d) { return Bloodhound.tokenizers.whitespace(d.num); },
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  remote: '/drugs/autocomplete?query=%QUERY'
});

drugSource.initialize();

$('#drug_search').typeahead(null, {
  name: 'drug',
  source: drugSource.ttAdapter()
});

有关v0.10.0版本更改内容的更多详细信息,请参阅migration guide