AutocompleteRails

AutocompleteRails is a lightweight component with easily understandable, minimal, source code. There are
other autocomplete gems out there that support multiple ORMs and provide client-side javascript, at the cost
of increased complexity. AutocompleteRails only supports ActiveRecord, and only provides rails
controller functionality. Client side, you use jQuery UI's autocomplete widget, which you wire up yourself.

Install

Use

Once the gem is installed, there are 3 steps to set up an autocomplete:

call autocomplete in your controller

add a route to your newly generated autocomplete method

provision an input in your UI with jQuery UI's autocomplete widget

Controller

Any controller needing an autocomplete action should invoke class method autocomplete with the model class and
method to be autocompleted as arguments. An autocomplete method is generated. Then add a route leading to your
generated method.

For example, to autocomplete users by email address in a Posts controller:

classPostsController<ApplicationControllerautocomplete:user,:emailend

autocomplete :user, :email creates a method on PostsController named autocomplete_user_email.

Routes

Add a route to your autocomplete action. For the controller listed above, you might add:

resources:postsdoget:autocomplete_user_email,on::collectionend

Wire up jQuery UI's autocomplete widget

jQuery UI's autocomplete widget is flexible and well documented. The most basic setup: just set the widget's
source option to the autocomplete rails controller path you wish to access. A common way to do
this is by setting the path to the autocomplete action in a data attribute of the input, and access it from
coffeescript/javascript. For example:

A simple jQuery event handler will attach autocomplete to any input[data-autocomplete]. Pass
in your autocomplete url as the source to jQuery's autocomplete widget. Here's an example (using turbolinks)
that adds the autocomplete widget to the input field show above: