Pat Shaughnessy

Ribadesella, Spain

The auto_complete plugin refactored to support repeated fields and named scopes

November 18, 2008 · 8 comments

(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:

Tags:·

8 responses so far ↓

  • 1 Bob // Nov 20, 2008 at 03:04 PM

    This is just what I need but I can't get it to work. After installing the plugin and reading your previous posts I get the following error: undefined method `text_field_with_auto_complete' for #<actionview::helpers::formbuilder:0x22fb654> I don't think I am the only one. What am I doing wrong?
  • 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

    Not able to figure out but not able to get rid of this error : missing ; before statement for file prototype.js(853) and edit(91) var author_-611411818_name_auto_completer = new Ajax.Autocompleter('author_-611411818_name', 'author_-611411818_name_auto_complete', '/books/auto_complete_for_author_name', {method:'get', paramName:'author[name]'})
  • 4 pat // Dec 09, 2008 at 12:18 AM

    Oops - looks like you have negative values for the object ids I inserted to insure unique input tag id attributes. Sorry my mistake. I updated github to use Object.new.object_id.abs instead on line 18. Can you pull/download from github, restart your server, reload the page and try again? Let me know if you still have trouble. Negative object ids don't appear on my Mac - are you using Windows by any chance?
  • 5 Kyle W. // Dec 10, 2008 at 03:50 AM

    Thnkx for the quick patch. Your plugin works like charm :)
  • 6 Anth // May 12, 2009 at 06:35 PM

    Excellent plugin. Does this work with the new nested attributes feature in Rails 2.3?
  • 7 pat // May 13, 2009 at 02:58 PM

    Nope... sorry looks like the changes in Rails 2.3 break my code. In Rails 2.3, instead of using fields_for directly in the view, you call it as a method on the parent form builder object… see: http://weblog.rubyonrails.org/2009/1/26/nested-model-forms. When I have time, I’ll have to refactor my changes to allow you to call auto_complete_fields_for on the form builder object, as well as from the view directly. Or it might be simpler just to allow text_field_with_auto_complete to work from any form builder object inside fields_for or form_for without using “auto_complete_fields_for” or “auto_complete_form_for.”

    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

    Thansk! This version of auto_complete is exactly what is needed to mix nested forms with auto_complete. Good work!

Leave a Comment