Today I try to find so many ways to do the livesearch in Rails from the Internet. Finally I just it in the book in front of me. (which is “Rails Recipes”).

Actually it’s pretty easy to do the livesearch in rails. Because rails already provides us a plugin called “auto_complete”. if you are using rails 2.0 version you need to install it.

ruby script/plugin install auto_complete

That’s it. Now you are ready to go

First Step

– Include a javascript in the head of your page

<%= javascript_include_tag :defaults %>

NOTE auto complete using a defaults javascript

Second Step

– In your specific will include this

<%= text_field_with_auto_complete :user, :name, {:size => 15} %>

Note – for my first parameter is “user”, which is the name of my model.

– second parameter is the attribute that you need to auto complete in your model.

Last Step

– Finally we got to the last step which is pretty easy. we need to add one more line into our controller

auto_complete_for :user, :name

and that’s it. now you can have a livesearch in your web site.

Note – in the book said after you put the line in to your controller. the controller will automatically generate a method called “auto_complete_for_user_name”

livesearch

figure 1.1 livesearch

If you are not clear with auto_complete you can also look more in this web site http://wiki.rubyonrails.org/rails/pages/How+to+use+text_field_with_auto_complete

Created by “WhenURnotAround”