The auto_complete plugin refactored to support repeated fields and named scopes
(Updated June 2009)
This version of auto_complete will support text fields that are repeated more than once on a complex form. It allows you to call text_field_with_auto_complete on the form builder object yielded by fields_for or form_for. This will work for complex forms built with Rails 2.2 or earlier, and for the nested attributes feature introduced in Rails 2.3. Here's an example using nested attributes:
<% form_for @project do |project_form| %> <% project_form.fields_for :tasks do |task_form| %> <p> <%= task_form.label :name, "Task:" %> <%= task_form.text_field_with_auto_complete :name, {}, { :method => :get, :skip_style => true } %> </p> <% end %> <% end %>
It also allows you to provide a block to auto_complete_for in your controller that filters the drop down pick list in some custom way. For example, this block would display task names for the project the user had selected elsewhere on the same form, using a named scope by_project:
auto_complete_for :task, :name do | items, params | items.by_project(params['project']) end
Code: http://github.com/patshaughnessy/auto_complete
Install as a gem:
gem sources -a http://gemcutter.org sudo gem install repeated_auto_complete
… and in config/environment.rb:
Rails::Initializer.run do |config| … config.gem "repeated_auto_complete" … end
Install as a plugin:
script/plugin install git://github.com/patshaughnessy/auto_complete.git
More information:
- How to generate scaffolding for auto_complete on a complex form
- How the plugin works with Rails 2.3 nested attributes
- Recent usage changes (June 2009) to enable nested attribute support
- Detailed description of code changes
- Sample app showing how to use the plugin
- Explanation of how to use named scopes with auto_complete_for
- Related article about how to filter auto_complete pick lists
- Why the auto_complete plugin doesn’t work for repeated fields
- My experience writing unit tests for the modified plugin
- My original changes to auto_complete from October (no longer used)