工作 Twitter-typeahead 的例子?
Working twitter-typeahead example?
我正在尝试将 twitter-typeahead-rails
gem 安装到我的应用程序中。我遵循了几个不同的教程,但所有教程都会导致错误。
有人有这样的工作示例吗gem?
将 gem 指定为 Gemfile
中的依赖项:
# Gemfile
gem 'bootstrap-multiselect-rails'
清单中需要预先输入文件:
// app/assets/javascripts/application.js
//= require twitter/typeahead
//= require twitter/typeahead/bloodhound
Javascript:
// app/assets/javascripts/models_controller.js
// initialize bloodhound engine
var bloodhound = new Bloodhound({
datumTokenizer: function (d) {
return Bloodhound.tokenizers.whitespace(d.value);
},
queryTokenizer: Bloodhound.tokenizers.whitespace,
// sends ajax request to /typeahead/%QUERY
// where %QUERY is user input
remote: '/typeahead/%QUERY',
limit: 50
});
bloodhound.initialize();
// initialize typeahead widget and hook it up to bloodhound engine
// #typeahead is just a text input
$('#typeahead').typeahead(null, {
displayKey: 'name',
source: bloodhound.ttAdapter()
});
// this is the event that is fired when a user clicks on a suggestion
$('#typeahead').bind('typeahead:selected', function(event, datum, name) {
doSomething(datum.id);
});
查看:
<-- app/views/models/whatever.html.erb -->
<input type="text" id="typeahead">
路线:
# config/routes.rb
get 'typeahead/:query' => 'models#typeahead'
控制器:
# app/controllers/models_controller.rb
def typeahead
render json: Model.where(name: params[:query])
end
## note: the above will only return exact matches.
## depending on the database being used,
## something else may be more appropriate.
## here is an example for postgres
## for case-insensitive partial matches:
def typeahead
render json: Model.where('name ilike ?', "%#{params[:query]}%")
end
GET 请求 /typeahead/%QUERY returns json 格式为:
[
{
"name": "foo",
"id": "1"
},
{
"name": "bar",
"id": "2"
}
]
接受的答案不完全正确。
似乎有 2 个不同的 gem 做着大致相同的事情:
bootstrap-multiselect-rails
目前在 gem 存储库中的版本为 0.9.9,并且具有与 post 中提到的不同的资产需求结构。此 gem 的资产需要包含为:
In application.js:
//= require bootstrap-multiselect
In application.css:
*= require bootstrap-multiselect
关于 Git 的更多信息:https://github.com/benjamincanac/bootstrap-multiselect-rails
或者,twitter-typeahead-rails
gem,目前版本为 0.11.1,似乎需要按照已接受答案的其余部分所述使用和包含。
关于 Git 的更多信息:https://github.com/yourabi/twitter-typeahead-rails
这两个 gem 似乎是在撰写本文时大约 5-6 个月前的最后一次更新。
最后,Bloodhound JS中指定的远程URL不正确:
remote: '/typeahead/%QUERY'
需要
remote: {url: '/typeahead/%QUERY', wildcard: '%QUERY'}
希望这对某人有所帮助
我正在尝试将 twitter-typeahead-rails
gem 安装到我的应用程序中。我遵循了几个不同的教程,但所有教程都会导致错误。
有人有这样的工作示例吗gem?
将 gem 指定为 Gemfile
中的依赖项:
# Gemfile
gem 'bootstrap-multiselect-rails'
清单中需要预先输入文件:
// app/assets/javascripts/application.js
//= require twitter/typeahead
//= require twitter/typeahead/bloodhound
Javascript:
// app/assets/javascripts/models_controller.js
// initialize bloodhound engine
var bloodhound = new Bloodhound({
datumTokenizer: function (d) {
return Bloodhound.tokenizers.whitespace(d.value);
},
queryTokenizer: Bloodhound.tokenizers.whitespace,
// sends ajax request to /typeahead/%QUERY
// where %QUERY is user input
remote: '/typeahead/%QUERY',
limit: 50
});
bloodhound.initialize();
// initialize typeahead widget and hook it up to bloodhound engine
// #typeahead is just a text input
$('#typeahead').typeahead(null, {
displayKey: 'name',
source: bloodhound.ttAdapter()
});
// this is the event that is fired when a user clicks on a suggestion
$('#typeahead').bind('typeahead:selected', function(event, datum, name) {
doSomething(datum.id);
});
查看:
<-- app/views/models/whatever.html.erb -->
<input type="text" id="typeahead">
路线:
# config/routes.rb
get 'typeahead/:query' => 'models#typeahead'
控制器:
# app/controllers/models_controller.rb
def typeahead
render json: Model.where(name: params[:query])
end
## note: the above will only return exact matches.
## depending on the database being used,
## something else may be more appropriate.
## here is an example for postgres
## for case-insensitive partial matches:
def typeahead
render json: Model.where('name ilike ?', "%#{params[:query]}%")
end
GET 请求 /typeahead/%QUERY returns json 格式为:
[
{
"name": "foo",
"id": "1"
},
{
"name": "bar",
"id": "2"
}
]
接受的答案不完全正确。
似乎有 2 个不同的 gem 做着大致相同的事情:
bootstrap-multiselect-rails
目前在 gem 存储库中的版本为 0.9.9,并且具有与 post 中提到的不同的资产需求结构。此 gem 的资产需要包含为:
In application.js:
//= require bootstrap-multiselect
In application.css:
*= require bootstrap-multiselect
关于 Git 的更多信息:https://github.com/benjamincanac/bootstrap-multiselect-rails
或者,twitter-typeahead-rails
gem,目前版本为 0.11.1,似乎需要按照已接受答案的其余部分所述使用和包含。
关于 Git 的更多信息:https://github.com/yourabi/twitter-typeahead-rails
这两个 gem 似乎是在撰写本文时大约 5-6 个月前的最后一次更新。
最后,Bloodhound JS中指定的远程URL不正确:
remote: '/typeahead/%QUERY'
需要
remote: {url: '/typeahead/%QUERY', wildcard: '%QUERY'}
希望这对某人有所帮助