Topic: Rails 2 and Autocomplete - Absolute Beginners Guide

I am writing this post after 2 weeks of trying to figure out the auto_complete plugin for rails 2.  I am a RoR newb, and it has been extremely difficult to find out how to properly implement this plugin.  Also, as a RoR newb, I feel alot of tutorials "assume" alot, which can be difficult for a beginner.
  After reading dozens of tutorials on the ajax autocomplete plugin, I finally found one that had the "missing link".  http://trix.pl/blog/auto-complete-for-r … orial.html 
  But I would like to craft a tutorial that starts from the very beginning and doesn't assume anything(except that you have ruby,rails and mysql up and running already).  So here goes...

Create your rails project

rails demo -d mysql
cd demo

Create a scaffold

ruby script/generate scaffold customer name:string

Edit your database.yml file

cd config
edit database.yml
add you password to the demo_development password: field
save and exit

Create your database

cd..
rake db:create
rake db:migrate

Start your server

ruby script/server

Launch your web browser

127.0.0.1:3000/customers
click the new customer link and start adding some new customer names
once you have about 5 or 6 names in your database you can close the web browser and ctrl c in your console

So that was all the basic stuff to get out projects framework, heres where we will get the autocomplete functionality going

Back in your console

ruby script/plugin install auto_complete

Edit your customers_controller.rb
add line 2

class CustomersController < ApplicationController
  auto_complete_for :customer, :name

Edit your views/customers/new.html.erb
change line 8 to...
<h1>New customer</h1>

<% form_for(@customer) do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :name %><br />
    <%= text_field_with_auto_complete :customer, :name, {}, {:method => :get} %>
  </p>
  <p>
    <%= f.submit "Create" %>
  </p>
<% end %>
<%= link_to 'Back', customers_path %>


Edit your views/layouts/customers.html.erb
add line 2 right below the stylesheet_link_tag (this will add the javascript functionality to your view)
<%= stylesheet_link_tag 'scaffold' %>
<%= javascript_include_tag :defaults %>

Now for the part that took me 2 weeks to find
Edit your config/routes.rb

change

map.resources :customers

to
map.resources :customers, :collection => {:auto_complete_for_customer_name => :get }

Go back into your console

ruby script/server

Now when you go to create a new customer name, if you start to type an existing name you will see it in the autocomplete drop down, if you create a new name, it will add it.

Hope this works for others too.  From what I've seen while searching for the solution myself there are alot of beginners (like myself) not able to get this to work.  It seemed to all boil down to that routes.rb configuration.

Re: Rails 2 and Autocomplete - Absolute Beginners Guide

So... now that I have got autocomplete working.  I need to get it working with associations.  That is now where I am stumped.  So without spending another 2 weeks on it, hopefully someone can help.

real simply I've got my
"workorder" controller

class WorkordersController < ApplicationController
  auto_complete_for :workorder, :customer_name

"workorder" model
class Workorder < ActiveRecord::Base 
   
  belongs_to :customer
 
  def customer_name
    customer.name if customer
  end
 
  def customer_name=(name)
   self.customer = Customer.find_or_create_by_name(name) unless name.blank?
  end
end

"customer" model
class Customer < ActiveRecord::Base
 
  has_many :workorders
 
end

"workorder" view
<%= f.label :customer_name %><br />
<%= text_field_with_auto_complete :workorder, :customer_name, {}, {:method => :get} %>

so, what should I to my routes.rb?
I've tried
ActionController::Routing::Routes.draw do |map|
  map.resources :customers

  map.resources :workorders, :collection => {:auto_complete_for_workorder_name => :get }


and that doesn't work. because of the associations I can't figure out how to get it to work.  Please help.  Thanks so much.

Re: Rails 2 and Autocomplete - Absolute Beginners Guide

Hi

Here is the answer. http://railscasts.com/episodes/102

Cheer

/Thu

Re: Rails 2 and Autocomplete - Absolute Beginners Guide

I've watched that a hundred times.  Is it supposed to be compatible with Rails 2?  The video doesn't explain anything to do with the routes.rb, is that ok?  also, it doesn't explain where he saves that javascript file.  It seems hard to follow for a beginner.  Is there any place to download the source code of an example of this.  Thanks for your reply.

Re: Rails 2 and Autocomplete - Absolute Beginners Guide

it is indeed compatible with Rails 2.. the index.js.erb (which is the javascript file i think you are talking about) is in app/views/categories/index.js.erb because it is the index action of the Categories controller that calls it. Try and repeat what he is doing in the railscast. If you try out the code for yourself, you will know what is required and what is not. As far as i know, no change is needed in the routes.rb.
There is also a plugin called model auto completer by Xavier Noria which does this. Good luck!

Last edited by yourstruly_vinay (2008-10-11 00:12:36)

Re: Rails 2 and Autocomplete - Absolute Beginners Guide

thanks for your reply, I will try it again.  I did look at the model auto completer but there wasn't really any documentation, and I really am a beginner.  But that railscast looks great.  I just couldn't get it to work originally, but I will try it again.  thanks again.

Re: Rails 2 and Autocomplete - Absolute Beginners Guide

well, I was able to get it to work this time.  found out my problem. in my customers controller that was generated by a scaffold.  once I commented out the respond_to "stuff" it works fine.

def index
    @customers = Customer.find(:all, :conditions => ['name LIKE ?', "%#{params[:search]}%"])

   # respond_to do |format|
   #   format.html # index.html.erb
   #   format.xml  { render :xml => @customers }
   # end
  end


thanks for the nudge to try it again wink

Re: Rails 2 and Autocomplete - Absolute Beginners Guide

ahh... uncommented out the respond_to stuff and added format.js to the list.  works perfect.

 def index
    @customers = Customer.find(:all, :conditions => ['name LIKE ?', "%#{params[:search]}%"])

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @customers }
      format.js
    end
  end

Last edited by richardscott (2008-10-12 08:35:00)

Re: Rails 2 and Autocomplete - Absolute Beginners Guide

great smile

Re: Rails 2 and Autocomplete - Absolute Beginners Guide

how would this work if the customer name isn't unique?

Re: Rails 2 and Autocomplete - Absolute Beginners Guide

Would appreciate it if you could extend this feature and show us how we can accomplish the following:

If a user starts typing the customer name and a list of customer names exists with those characters, simply show the available customer names with a link at the bottom of the list, showing create new customer option, and when clicked, hide the autocompleter field box and show a new customer form. If the customer names doesn't exist, show the new customer form automatically so that the user knows that he has to fill the new customer form. www.plazes.com has implemented this feature when you are adding a new activity (http://plazes.com/activities/new).

Thanks in advance.
_Satynos

Last edited by satynos (2008-12-22 12:53:30)

Re: Rails 2 and Autocomplete - Absolute Beginners Guide

here is also an example on how to add auto complete in RoR http://share-facts.blogspot.com/2009/02 … rails.html

Re: Rails 2 and Autocomplete - Absolute Beginners Guide

I have a question about this!

Let's say I've got a scaffold called User, with the columns first_name and last_name. How would I'd do if I wanted auto_complete to show the full name when writing in a field?

Re: Rails 2 and Autocomplete - Absolute Beginners Guide

Change the 'auto_complete_result' method in your index.js.erb file:
index.js.erb before:

<%=  auto_complete_result @users, :lastname%>

This way you see only user name. If you change :name for some helper that sends you full description.

Re: Rails 2 and Autocomplete - Absolute Beginners Guide

Hi everyone, i got a question about this plugin.

Let's say i want to auto_complete users with the first_name. When i select the first_name, i want the last name to appear in another text_field.

im really a newb with this plugin and rails, so be gentle!

Re: Rails 2 and Autocomplete - Absolute Beginners Guide

I m not Getting..
if i want to display user name with first_name and last_name than how i can display full user name in one  text field.