Skip to content

Rails plugin for auto_complete with jQuery instead of Prototype/Scriptaculous

Notifications You must be signed in to change notification settings

mreinsch/auto_complete_jquery

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This plugin provides a auto-complete method for your controllers to be used with Dylan Verheul’s jquery autocomplete plugin. This jQuery plugin is not included, and can be obtained from:

bassistance.de/jquery-plugins/jquery-plugin-autocomplete/

This auto_complete_jquery plugin is just a modified version of the standard Rails auto_complete plugin. It provides the same auto_complete_for method for your controllers, but eliminates the various view helper methods, as those are not needed when using jQuery and Unobtrusive JavaScript.

To use this, you need to have jQuery and the autocomplete plugin mentioned above (as well as appropriate CSS). Then, there are four aspects of setting up an auto-complete field:

1) Create the text field in your view, which is just a regular form text field as you’d create in a Rails erb view:

<%= post.text_field :title, :autocomplete =>"off" %>

2) Include the appropriate JS files and CSS in your layout or similar:

<%= stylesheet_link_tag 'jquery.autocomplete' %>
<%= javascript_include_tag 'jquery.min', 'jquery.autocomplete.min', :cache => 'jquery' %>

3) Use the auto_complete_for controller macro to generate the appropriate auto-complete method in your controller, such as:

# Controller
class BlogController < ApplicationController
  auto_complete_for :post, :title
end

4) Hook up the text field’s autocomplete event in your JavaScript file using jQuery and the jquery-autocomplete plugin mechanism, for example:

$(document).ready(function() {
  $("input#post_title").autocomplete({ajax: "/blog/auto_complete_for_post_title"})
});

By default, auto_complete_for limits the results to 10 entries, and sorts by the given field.

auto_complete_for takes a third parameter, an options hash to the find method used to search for the records:

auto_complete_for :post, :title, :limit => 15, :order => 'created_at DESC'

5) Create a route for the auto complete function. You can add the following generic route at the beginning of your config/routes.rb to take care of this:

map.auto_complete ':controller/:action',
  :requirements => { :action => /auto_complete_for_\S+/ },
  :conditions => { :method => :get }

For more information, see:

Copyright © 2008 Cobalt Edge LLC, released under the MIT license. Copyright © 2009 mobalean LLC, released under the MIT license.

About

Rails plugin for auto_complete with jQuery instead of Prototype/Scriptaculous

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Ruby 100.0%