The bootstrap-typeahead-rails
gem’s README kicks the question over to Twitter’s typeahead.js README. This left much to be desired.
This Stack Overflow answer provides detailed instructions for the twitter-typeahead-rails
gem. I wanted to see something like that for the bootstrap-typeahead-rails
gem.
2
Answers
Here's my guide. It follows @ihaztehcodez's example. This example assumes a model
Thing
and adds a form to the index view for searchingthings
by the model'sname
attribute.A few notes:
Add gem to gemfile
Include typeahead files in asset manifests
Stylesheet (SASS)
Javascript
Add typeahead route to routes file
Add typeahead javascript code
Add relevant methods/actions to controller
Add search form to index view (using SLIM gem)
Create Searchlight search class
If you prefer not to use Searchlight, use the ActiveRecord query interface in the model.
@klenwell’s answer is out of date. Here’s how I got it to work:
I’m using:
My model is called
Destination
.app/models/destination_search.rb:
controller:
JS:
and in the view:
<%= text_field_tag :destination, class: "destination-typeahead" %>
It feels a little bit hacky how I’m passing the
process
method intobloodhound.search
twice – this is because bloodhound#search takes two callbacks as arguments, one that deals with cached/prefetched data and one that deals with data pulled dynamically via AJAX. I might not be using #search 100% correctly, but this approach works, and it’s a simple start.