.

multiple_auto_complete plugin

Rails’ auto_complete control can be a really handy way to allow users to select an object or string from a list of pre-existing values, however, one big limitation to its use is the fact that you can’t have more than one of them on a single page.

UPDATE: just to clarify this a bit. If you have more than one auto_complete that tries to access the same model/field combo, you get into trouble. You can have multiple auto_completes so long as they access different model/field combinations.

I tried a couple of hacks around this problem (some involving method_missing), and finally found a method that works pretty well and doesn’t mess up your controllers.

The approach I ended up using was to rewrite the ‘auto_complete_for’ and ‘text_field_with_auto_complete’ functions so that they ignore trailing numbers on a dom_id. Once you install the plugin (see below), you simply use it like this…

  1. # View file
  2. <%= text_field_with_auto_complete ‘object_1′, ‘name’ %>
  3. <%= text_field_with_auto_complete ‘object_2′, ‘name’ %>

  1. # controller file
  2. auto_complete_for :object, :name

All the auto_complete fields named /object[-_]d+/ will call the ‘auto_complete_for_object_name’ function in the controller now. The default response function will pull out the correct information and respond as usual.

When you submit the form, the values will be passed in the params hash as

  1. params = { ‘object_1′ => {‘name’=>’name1′}, ‘object_2′=>{‘name’=>’name2′ }}

Installation

script/plugin install svn://sciwerks.com/home/sciwerks/public_svn/multiple_auto_complete/

7 Responses to “multiple_auto_complete plugin”

  1. September 26th, 2006 | 8:12 am

    multiple_auto_complete plugin…

    “Rails auto_complete control can be a really handy way to allow users to select an object or string from a list of pre-existing values, however, one big limitation to its use is the fact that you cant have more than one of them on a single page.”…

  2. October 2nd, 2006 | 1:18 pm

    [...] [...]

  3. October 18th, 2006 | 2:56 am

    I think a better solution is to use :index, and the patch described at http://blog.wolfman.com/articles/2006/10/17/having-multiple-text_field_with_auto_complete-in-the-same-view
    to solve this problem, hopefully this patch will get into edge rails someday.
    This ticket is still open http://dev.rubyonrails.org/ticket/5983 which also shows the fix.

  4. October 18th, 2006 | 7:37 am

    Jim,
    That’s a pretty good solution.

  5. December 12th, 2006 | 4:26 pm

    What I want to do on my blog, is every few hours take the oldest post and move it to the
    front of the queue, all automatically. Anyone know if there is a plugin that can do this or
    a simple way to set up another plugin to do this (use my own feed perhaps)?
    Thanks.

  6. Chad
    March 8th, 2007 | 12:18 pm

    The SVN repo for this plugin seems to be offline. Is there another way to get it, or does someone else have a solution that works?

    Thanks,

    Chad

  7. March 8th, 2007 | 12:54 pm

    The SVN repository for this is currently offline. I will be moving it to rubyforge shortly, but until then I will email you a copy of the files to anyone who needs them.

Leave a reply