views:

176

answers:

2

I want to use auto-completion in a number of fields (5-7) in my forms. There is a screencast on auto-completion with Prototype library by Ryan Bates ( http://railscasts.com/episodes/102-auto-complete-association). On the other hand, I have noticed that quite many guys suggest jQuery for this task ( http://jquery.bassistance.de/autocomplete/demo/). And I guess, there was probably some development last year(s), so I ask you - what would you use nowadays to auto-complete your form fields and why?

BTW, I still have an open question on auto completion for HABTM association: http://stackoverflow.com/questions/1510935/how-to-do-habtm-management-with-auto-completion-in-rails

+1  A: 

Disclaimer: This answer covers only the jQuery half of the question, as I have no experience with the Prototype autocomplete plugins, I'll leave that for someone with expertise to answer.

First, I would stick with a plugin written in the framework you're already using, there's no need to switch from prototype to jquery or vice-versa for this.

If you're not already using a framework or are using jQuery, I'd suggest the jQuery UI autocomplete (different from the one you linked) mainly around the support and extensibility it offers and will offer.

It doesn't matter which framework you're looking at, once a plugin forms or gets pulled into any core library (jQuery UI in this case) it gets a wealth more support and the community more readily builds on it, making it better and more extensible. Also if you run into trouble, you're more likely to find an answer to your problem.

For the rails portion, there's are articles popping up on how to use this autocomplete plugin specifically with rails, I'd get started here.

Nick Craver
+2  A: 

I use the YUI auto-complete. Industrial strength, used on Yahoo, Flickr and other grade-A web properties.

Performance

In Rails, for the best performance on Auto-complete (no matter which widget you use), you should create a View in your database to your lookup information. Eg you want to use Autocomplete to enable the human to quickly search his posts by title (not by content).

Create a View:

# Rake task code for creating a view
def self.search_posts # 
  execute "DROP Table IF EXISTS search_posts"
  execute "DROP View IF EXISTS search_posts"
  execute "CREATE View search_posts AS
  SELECT
     p.id
  ,  p.title
  ,  p.user_id
  FROM posts p
  ORDER BY p.title
  "    
end

Also create an ActiveRecord model for SearchPost. It will pull from the view.

Then, in your controller:

# Return the id's and title's matching the search query
# Assumptions: current user id is in Session[:user_id]
#              Auto complete query is in params[:query]
SearchPost.find_all(
  :conditions => ["user_id = ? and title LIKE ?",
                  Session[:user_id], "%#{params[:query]}%"]
  ).collect{|rec| {'title' => rec.title, 'id' => rec.id}.to_json

Benefit

You want to minimize the amount of data looked up via the db for the Ajax requests that the auto-complete widget sends you.

Local Auto-complete

Another auto-complete architecture is the one that Flickr uses: download your entire db of post titles and ids to the browser and search against them locally. See http://code.flickr.com/blog/2009/03/18/building-fast-client-side-searches/

Larry K
+1. I recommend you to read the last link, it's very nice.
Thiago Silveira