auto_complete plugin refactored to handle complex forms and named scopes
Switch branches/tags
Nothing to show
Clone or download
#6 Compare This branch is 21 commits ahead of rails:master.
Failed to load latest commit information.
rails repeated_auto_complete gem and view_mapper module Nov 25, 2009
.gitignore repeated_auto_complete gem and view_mapper module Nov 25, 2009
VERSION Suggested by Mike Wicks: use entry.send(method) in auto_complete_result Mar 2, 2010


Repeated Auto Complete

Fork of the standard auto_complete plugin to support:

  1. Creating auto complete text fields that can be repeated more than once on a single form.

  2. Using auto_complete_for with named scopes to display a customized list of autocomplete options.

See: for details.

Install as a gem

gem sources -a
sudo gem install repeated_auto_complete

and in config/environment.rb: do |config|

  config.gem "repeated_auto_complete"


Install as a plugin

script/plugin install git://

Repeated autocomplete text fields:

A “text_field_with_auto_complete” method is made available to the form builder yielded by form_for and fields_for that:

  • Insures unique id's for <input> and <div> tags by inserting unique integers into their id attributes

  • Uses the object name from the surrounding call to form_for or fields_for so attribute mass assignment will work as usual

  • Works with the same server side controller method, auto_complete_for, as usual

  • Supports nested attributes in Rails 2.3

form.text_field_with_auto_complete works the same as the original text_field_with_auto_complete macro, except it does not take the object as a parameter.

Example with nested attributes using Rails 2.3 or later:

class Project < ActiveRecord::Base
  has_many :tasks
  accepts_nested_attributes_for :tasks, :allow_destroy => true

<% form_for @project do |project_form| %>
    <%= project_form.label :name, "Project:" %>
    <%= project_form.text_field_with_auto_complete :name, {}, {:method => :get } %>
  <% project_form.fields_for :tasks do |task_form| %>
      <%= task_form.label :name, "Task:" %>
      <%= task_form.text_field_with_auto_complete :name, {}, { :method => :get, :skip_style => true } %>
  <% end %>
<% end %>

Rails 2.2 and earlier example:

<% for person in @group.people %>
  <% fields_for "group[person_attributes][]", person do |person_form| %>
      Person <%= person_form.label :name %><br/>
      <%= person_form.text_field_with_auto_complete :name, {}, {:method => :get }  %>
  <% end %>
<% end %>

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 :some_model, :some_other_field do |items, params|
  items.scoped( { :conditions => [ "a_third_field = ?", params['some_model']['a_third_field'] ] })

Example using named scope:

class Task < ActiveRecord::Base
  belongs_to :project
  named_scope :by_project,
    lambda { |project_name| {
      :include => :project,
      :conditions => [ " = ?", project_name ]
    } }

auto_complete_for :task, :name do | items, params |

Autocomplete Scaffolding:

The “View Mapper” gem will generate scaffolding code that illustrates how to use the standard Rails auto_complete plugin in a simple form, or the repeated_auto_complete plugin/gem in a complex form.



Copyright © 2009 [Pat Shaughnessy], released under the MIT license