(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)
8 responses so far ↓
1 Bob // Nov 20, 2008 at 03:04 PM
2 Pat // Nov 20, 2008 at 08:31 PM
It looks like you're using the standard "form_for" or "fields_for" method which yields the FormBuilder class that doesn't have the new text_field_with_auto_complete method. Can you double check your view? Be sure to call "auto_complete_form_for" or "auto_complete_fields_for," which will yield the RepeatedAutoCompleteFormBuilder class and you'll get the methods you need. Let me know if this doesn’t work for you.
I can imagine this is confusing… maybe a better design would be to open the default FormBuilder class itself, instead of creating new form helper methods with unfamiliar names. I'll try this out next week and write here about how it feels.
3 Kyle W. // Dec 08, 2008 at 12:19 PM
4 pat // Dec 09, 2008 at 12:18 AM
5 Kyle W. // Dec 10, 2008 at 03:50 AM
6 Anth // May 12, 2009 at 06:35 PM
7 pat // May 13, 2009 at 02:58 PM
I’ll try to write a new blog post soon about how to use auto complete in Rails 2.3...
8 Mario Leyton // Sep 15, 2009 at 09:24 AM
Leave a Comment