jQuery autocomplete

Rails Auto Completion plugin using jQuery-UI1; it works similarly to the original rails auto complete, but generating unobtrusive javascript. Additional features:

  • named scope items filtering, based on a customized list of autocomplete options.

WARNING NOTE: This is an active work in progress – currently only used on few live projects right now. Use at your own discretion.

Usage example

In the controller you can use the macro function as such

class ExampleController < ApplicationController
    auto_complete_for :fruit, :name
    ...
end

In your views:

<%= text_field_with_auto_complete :fruit, :name %>

be sure to include the jquery/jquery-ui javascript in your layout:

<%= javascript_include_tag 'jquery, 'jquery-ui.min', 'jquery-ui-i18n.min' %>

Named scopes with auto_complete_for:

auto_complete_for now optionally accepts a block that is called with the item list and HTTP parameters when the auto complete AJAX request is received. This block can be used to specify that a named scope be used to generate a customized list of autocomplete options.

Example using anonymous scope:

auto_complete_for :fruit, :shape do |items, params|
  items.scoped( { :conditions => [ "colour = ?", params['fruit']['color'] ] })
end

Example using named scope:

Having the model:

class Fruit < ActiveRecord::Base
  belongs_to :owner
  named_scope :by_owner,
    lambda { |owner_name| {
      :include => :owner,
      :conditions => [ "owner.name = ?", owner_name ]
    } }
end

In the controller you can use the macro function as such

auto_complete_for :fruit, :name do | items, params |
  items.by_owner(params['owner'])
end

Todo

  • test, test… test!

Found a Bug?

Please direct all queries to the issue tracker: http://github.com/michelefranzin/jrails_auto_complete/issues

Credits

Thanks to Paul Smith for the original idea and Pat Shaughnessy for inspirating me the named scope part.

References

1 jQuery-UI

Copyright

Copyright © 2010 Michele Franzin. See LICENSE for details.