Define a pearl as a dependency in your Gemfile:
gem 'bootstrap-multiselect-rails'
Require head type files in your manifest:
JavaScript:
var bloodhound = new Bloodhound({
datumTokenizer: function (d) {
return Bloodhound.tokenizers.whitespace(d.value);
},
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: '/typeahead/%QUERY',
limit: 50
});
bloodhound.initialize();
$('#typeahead').typeahead(null, {
displayKey: 'name',
source: bloodhound.ttAdapter()
});
$('#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:
def typeahead
render json: Model.where(name: params[:query])
end
def typeahead
render json: Model.where('name ilike ?', "%#{params[:query]}%")
end
A GET request in / typeahead /% QUERY returns json in the form:
[
{
"name": "foo",
"id": "1"
},
{
"name": "bar",
"id": "2"
}
]
source
share