AutoCompleter example in Ruby on rails

by Noman Muhammad

in Ruby on Rails adding auto complete textbox is pretty simple. here is a simple example of it ...
in this example there will be 2 tables in DB. Customers table & Messages table. In the Customers table customer name and address will be saved. In the Messages table customer name and message to that customer will be saved. In the message form if we start typing the customer name, the name of the customers will be shown and we can select a particular customer name. that all. so lets start ...

Here i use Netbeans 6.0 as IDE

1. first create a new rails project.
2. run Rake Task to create the db
3. generate scaffold with this parameter in the [Model Name:] text field

Customer name:string address:text
4. run Migrate Database to create the customers table
5. now run this project
6. in the browser go to http://localhost:3000/customers and add some customers name and address
7. now generate another scaffold with this parameter in the [Model Name:] text field
Message to:string body:text
8. run Migrate Database to create the messages table
9. now the plugin Auto_complete have to be downloaded
10. download auto_complete plugins from github
11. unzip the downloaded zip folder and rename the folder 'auto_complete'
11. store 'auto_complete' folder in the "\vendor\plugins\" folder. [i.e. \vendor\plugins\auto_complete]
12. now, open the file 'messages.html.erb' from \app\views\layouts and add the following line before tag
<%= javascript_include_tag :defaults %>
13. now, open the file 'new.html.erb' from app\views\messages and change this line
<%= f.text_field :to %>
to
<%= text_field_with_auto_complete 'message', 'to',{}, :skip_style => false  %>
14. open MessageController and add this line
skip_before_filter :verify_authenticity_token, :only => [:auto_complete_for_message_to]
after this
class MessagesController < ApplicationController

15. add this new function in this controller:
def auto_complete_for_message_to()
user_name = params[:message][:to]
@customers = Customer.find(:all , :conditions=>"name like '%"+user_name.downcase+"%'")
render :partial => 'username'
end

16. now create a new partial file in app\views\messages and name it _username.html.erb
17. paste this code in this partial file
<ul class="allusers"><% for customer in @customers do %><li class="thisuser"><div class="useremail"><%=h customer.name %></div><div class="username"><span class="informal"><%=h customer.address %></span></div></li><% end %></ul>

18. now restart server and in the browser go to http://localhost:3000/messages and now in the to field start typing u'll see some name coming from DB.
19. so its woking !!!!!!!!!!!!!!!!!!!!!!!!!!!

This entry was posted on Thursday, February 12, 2009 and is filed under , . You can leave a response and follow any responses to this entry through the Subscribe to: Post Comments (Atom) .

15 comments

Thanks a lot!! I was searching for days for an example such as this (that will work with Rails 2.2.2 and the latest)!!!

Now to make it work without an IDE... :-)

Anonymous  

Hi, i've implemented the above code and restarted the server. I'm using the rails version 2.2.2. But i'm getting the following error:

undefined method `text_field_with_auto_complete' for # ActionView::Base:0x4868720

Please help.....

do u place auto_complete folder in the right directory? can u send me ur full code in zip format? my email: noman13bd at gmail dot com

Anonymous  

Hi Norman,
Great tutorial, thanks! I got the auto-complete working on my site in less than 10 mins.

If I wanted the user to be able to press Enter and get taken to the 'show' page for a specific result from the database, how would I implement this? I've tried using the link_to helper, but it only works when they click the result from the auto-complete drop-down div.

Your help would be great.
Thanks!
David

@David: can u be more specific whad u want to do?
thnx

Anonymous  

Hi Norman

Sorry I'm having difficulty explaining. I'll start again :)

I've got the auto complete working for my 'Contact Management' App. Thanks.

When I type, it creates the drop down of contacts names from the DB which is great.

When I click on a result from the drop down, it fills in the auto complete text box. Which is also fine.

From here, if the user presses "Enter" with the contact name in the search box , I would like it to goto that contacts details page.

Do you have any suggestions on how to integrate this?

Thanks again!

@David: its very simple and easy. Make a action for this in the controller and then for the 'Enter' button link to this action that will retrieve the contact details. If u not understand mail me.... hopefully i can give u the solution explicitly. Thnx

I could not see code on step no. 12 and 13. I guess step 12 is to add javascript tag. But in step 13 i added text_field_for_auto_complete and didnt work. Please explain for me.

in step 13 do:

open the file 'new.html.erb' from app\views\messages and change this line

<%= f.text_field :to %>

to

<%= text_field_with_auto_complete 'message', 'to',{}, :skip_style => false %>

Thanks for this instructions.

Anonymous  

You rock!!! Thanks for posting!

Anonymous  

thank for this

Anonymous  

good post.very useful.thankyou.and it would be fine if you give explanation also

Anonymous  

It would be a good post, but why i can't see any code in 12 and 13 tasks??

Anonymous  

Goodone !!!

Minal :):)