I’ve forked the auto_complete plugin to support repeated text fields in a complex form; see http://patshaughnessy.net/repeated_auto_complete for more details.
If you had downloaded my plugin in the past, I’ve just made a couple of changes that will require some simple code changes to your app:
- You no longer need to or are able to use “auto_complete_form_for” or “auto_complete_fields_for.” I decided this was confusing and unnecessary. Now my plugin just mixes the text_field_with_auto_complete method right into the standard FormBuilder class. Just use form_for or fields_for as usual.
- I also dropped the object name parameter from text_field_with_auto_complete. Since text_field_with_auto_complete is a method of the form builder, the target object is indicated by the surrounding call to fields_for or form_for and so doesn’t need to be repeated. Now using form.text_field_with_auto_complete is very similar to using form.text_field or the other form builder methods: you just need to specify the column/field name.
So if you are using my old plugin with a Rails 2.2 or earlier app like this:
<% for person in @group.people %>
<% auto_complete_fields_for "group[person_attributes][]", person do |form| %>
Person <%= person_form.label :name %><br />
<%= form.text_field_with_auto_complete :person, :name, {},
{:method => :get} %>
<% end %>
<% end %>
… you should drop “auto_complete_” and “:person” and just use code like this instead:
<% for person in @group.people %>
<% fields_for "group[person_attributes][]", person do |form| %>
Person <%= person_form.label :name %><br />
<%= form.text_field_with_auto_complete :name, {},
{:method => :get} %>
<% end %>
<% end %>
And if you have Rails 2.3 or later and are using nested attributes, this would become:
<% form_for @group do |group_form| -%>
<% group_form.fields_for :people do |person_form| %>
Person <%= person_form.label :name %><br />
<%= person_form.text_field_with_auto_complete :name, {},
{ :method => :get, :skip_style => true } %>
<% end %>
<% end %>
2 responses so far ↓
1 Mike Wicks // Feb 26, 2010 at 12:34 PM
2 pat // Feb 27, 2010 at 09:44 AM
Hey thanks a lot for trying my gem; sorry you had to waste an afternoon on this issue! I really appreciate knowing about this auto_complete_result fix… thanks for the heads up. It looks good and I’ll merge it into my version after writing a couple of tests for this behavior. Probably the reason the original auto_complete plugin calls the field and not the method is that the “auto_complete_for” helper method performs an actual SQL query to get the entries (see my article Getting started with Ruby metaprogramming for more info.) But changing auto_complete_result makes sense because probably a lot of people are using auto_complete_result but not auto_complete_for.
Leave a Comment