What would you use for autocomplete in a Rails application?

I want to use autocomplete on multiple fields (5-7) in my forms. There is a screencast on autocomplete with the Prototype

Ryan Bates library ( http://railscasts.com/episodes/102-auto-complete-association ). On the other hand, I noticed quite a few guys suggest jQuery

for this task ( http://jquery.bassistance.de/autocomplete/demo/ ). And I think there has probably been some development in the past year, so I'm asking you - what would you use to auto-complete form fields now and why?

BTW, I still have an open-ended auto-completion question for the HABTM association: How do I make HABTM control auto-complete in Rails?

+2


a source to share


2 answers


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

Firstly, I would stick with a plugin written in the framework you are already using, there is no need to switch from prototype to jquery or vice versa for this.

If you are not using a framework yet, or using jQuery, I would suggest jQuery UI autocomplete (other than the one you linked) mostly around the support and extensibility it offers and will offer.



It doesn't matter which framework you look at, once a plugin is built or pulled into any core library ( jQuery UI in this case), it gets a lot of support and the community leans on it more easily, making it better and more extensible. Also, if you run into problems, you are more likely to find the answer to your problem.

For the rails part, there are articles on how to use this autocomplete plugin specifically with rails.I would start here .

+1


a source


I am using YUI autocomplete . Industrial strength used by Yahoo, Flickr and other websites.

Performance

In Rails, for maximum auto-completion performance (no matter which widget you are using), you must create a view in your database for your search information. For example, you want to use Autocomplete to allow a person to quickly find their entries by title (not content).

Create 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. He will pull out of sight.



Then in your controller:

# Return the id and title 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 viewed through the db for Ajax requests that the autocomplete widget sends you.

Local auto-completion

Another autocomplete architecture is the one Flickr uses: load your entire db of post headers and ids into the browser and find them locally. See http://code.flickr.com/blog/2009/03/18/building-fast-client-side-searches/

+2


a source







All Articles