当我输入文本字段时,我没有看到任何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"
答案 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。