I'm trying to install the twitter-typeahead-rails
gem into my app. I've followed several different tutorials but all of them result in errors.
Does anyone have a working example of this gem?
Specify gem as dependency in your Gemfile
:
# Gemfile
gem 'bootstrap-multiselect-rails'
Require typeahead files in your manifest:
// 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);
});
View:
<-- app/views/models/whatever.html.erb -->
<input type="text" id="typeahead">
Routes:
# config/routes.rb
get 'typeahead/:query' => 'models#typeahead'
Controller:
# 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 request to /typeahead/%QUERY returns json in the form of:
[
{
"name": "foo",
"id": "1"
},
{
"name": "bar",
"id": "2"
}
]
The accepted answer is not completely correct.
There seem to be 2 different gems doing roughly the same thing:
bootstrap-multiselect-rails
is currently on version 0.9.9 in the gem repository and has a different asset require structure as mentioned in the post. This gem's assets need to be included as:
In application.js:
//= require bootstrap-multiselect
In application.css:
*= require bootstrap-multiselect
More on Git: https://github.com/benjamincanac/bootstrap-multiselect-rails
Alternatively, there is the twitter-typeahead-rails
gem, currently on version 0.11.1 which seem to need to be used and included as described in the rest of the accepted answer.
More on Git: https://github.com/yourabi/twitter-typeahead-rails
Both gems seem to be last updated about 5-6 months ago at the moment of writing this.
Finally, the remote URL specified in the Bloodhound JS is incorrect:
remote: '/typeahead/%QUERY'
Needs to be
remote: {url: '/typeahead/%QUERY', wildcard: '%QUERY'}
Hopefully this helps someone
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With