problem with auto_complete!!

Hello!!!

    first of all sorry for my English! :frowning:

   Well I'm trying to make an auto_complete in my application, but it's
getting hard to do it. This is all what I've done:

1.I'm working with rails 2.1 and Postgres
2.My model name is: Alumno
3.My record name is: nombre in Alumno tables
4.I install the pluging with this command
(http://wiki.rubyonrails.org/rails/pages/How+to+use+text_field_with_auto_complete):

   script/plugin install auto_complete

5.in my view (example.rhtml)
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"
/>
    <title>Mi boleta en la WEB</title>
    <%= javascript_include_tag :defaults %>
    <%= javascript_include_tag 'prototype' %>
    <%= javascript_include_tag "scriptaculous/prototype" %>
    <%= javascript_include_tag "scriptaculous/scriptaculous" %>
    <%= javascript_include_tag 'scriptaculous' %>

    <%= stylesheet_link_tag "example" %>
    <%= javascript_include_tag :all, :cache => true %>

</head>
    <body>

<%= text_field_with_auto_complete :alumno, :nombre, { :size => 15 },
:skip_style => true -%>

   </body>
</html>

6. my model (alumno.rb)
ActiveRecord::Base.establish_connection(
        :adapter => 'postgresql',
        :host => 'localhost',
        :port => "5432",
        :username=> 'XXXXX',
        :password=> 'XXXXXX',
        :database=> 'boletin'
        );

class Alumno < ActiveRecord::Base

end

7.my controller(completar_controller.rb)
class CompletarController < ApplicationController
auto_complete_for :alumno, :nombre, :limit => 15
  def index

       @alumno = Alumno.new
       render :action => 'example'
    end

  def auto_complete_for_alumno_nombre
     search = params[:alumno][:nombre]
     @nombres = Alumno.find(search) unless search.blank?
     render :partial => "result"
  end

end

8.my partial (_result.rhtml)
<ul class="autocomplete_list">
  <% for nombre in @nombres .to_a -%>
    <li class="autocomplete_item"><%= nombre.nombre %>

<% end -%>

9.my CSS (complete.css)

.auto_complete {
      position:absolute;
      width:250px;
      background-color:white;
      border:1px solid #888;
      margin:0px;
      padding:0px;
}

.auto_complete ul {
      list-style-type: none;
      margin:0px;
      padding:0px;
}

.auto_complete ul li.selected
{
      background-color: #bbf;

}

.auto_complete ul li {
      list-style-type: none;
      display:block;
      margin:0;
      padding:2px;
      height:16px;
}

That's all I have. The problem is that, it doesn't do anything, I can't
see anything in my text_field_with_auto_complete, it doesn't show any
error, can someone help me please I really want to get it right..thank
you!! :slight_smile:

Is your Alumno controller setup as a RESTful resource? If so, then
the auto complete call won't work because the auto_complete url isn't
one of the default routes created with a RESTful resource. You could
explicitly add the route but I would suggest checking out this episode
of Railscasts:

http://railscasts.com/episodes/102-auto-complete-association

It gives a straightforward approach to creating an auto complete field
with a RESTful setup.

Greg DeVore

gdevore wrote:

Is your Alumno controller setup as a RESTful resource? If so, then
the auto complete call won't work because the auto_complete url isn't
one of the default routes created with a RESTful resource. You could
explicitly add the route but I would suggest checking out this episode
of Railscasts:

http://railscasts.com/episodes/102-auto-complete-association

Alumnos is my table and belongs to a data base call Prueba, so my model
is alumno.rb and my controller is completar_controller.rb, I will check
the page that you recommend me, and try to do some changes, if it work I
will tell you. Thanks

Download Firebug, it'll give you a much better idea of what's going on

http://getfirebug.com/

Juan Moreno wrote:

Hello!!!

    first of all sorry for my English! :frowning:

   Well I'm trying to make an auto_complete in my application, but it's
getting hard to do it. This is all what I've done:

1.I'm working with rails 2.1 and Postgres
2.My model name is: Alumno
3.My record name is: nombre in Alumno tables
4.I install the pluging with this command
(http://wiki.rubyonrails.org/rails/pages/How+to+use+text_field_with_auto_complete):

   script/plugin install auto_complete

5.in my view (example.rhtml)
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"
/>
    <title>Mi boleta en la WEB</title>
    <%= javascript_include_tag :defaults %>
    <%= javascript_include_tag 'prototype' %>
    <%= javascript_include_tag "scriptaculous/prototype" %>
    <%= javascript_include_tag "scriptaculous/scriptaculous" %>
    <%= javascript_include_tag 'scriptaculous' %>

    <%= stylesheet_link_tag "example" %>
    <%= javascript_include_tag :all, :cache => true %>

</head>
    <body>

<%= text_field_with_auto_complete :alumno, :nombre, { :size => 15 },
:skip_style => true -%>

   </body>
</html>

6. my model (alumno.rb)
ActiveRecord::Base.establish_connection(
        :adapter => 'postgresql',
        :host => 'localhost',
        :port => "5432",
        :username=> 'XXXXX',
        :password=> 'XXXXXX',
        :database=> 'boletin'
        );

class Alumno < ActiveRecord::Base

end

7.my controller(completar_controller.rb)
class CompletarController < ApplicationController
auto_complete_for :alumno, :nombre, :limit => 15
  def index

       @alumno = Alumno.new
       render :action => 'example'
    end

  def auto_complete_for_alumno_nombre
     search = params[:alumno][:nombre]
     @nombres = Alumno.find(search) unless search.blank?
     render :partial => "result"
  end

end

8.my partial (_result.rhtml)
<ul class="autocomplete_list">
  <% for nombre in @nombres .to_a -%>
    <li class="autocomplete_item"><%= nombre.nombre %>

<% end -%>

9.my CSS (complete.css)

.auto_complete {
      position:absolute;
      width:250px;
      background-color:white;
      border:1px solid #888;
      margin:0px;
      padding:0px;
}

.auto_complete ul {
      list-style-type: none;
      margin:0px;
      padding:0px;
}

.auto_complete ul li.selected
{
      background-color: #bbf;

}

.auto_complete ul li {
      list-style-type: none;
      display:block;
      margin:0;
      padding:2px;
      height:16px;
}

That's all I have. The problem is that, it doesn't do anything, I can't
see anything in my text_field_with_auto_complete, it doesn't show any
error, can someone help me please I really want to get it right..thank
you!! :slight_smile:

Hi Juan.

As I'm fairly new to RoR I may not be the right one to solve your
problem. But since you ask so direct here is what I did:

I got inspired by (more or less copied) this example:
http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/65f6f9011fa78b41/d45c3f04c2eacf4a?lnk=gst&q=text_field_with_auto_complete#d45c3f04c2eacf4a

Especially this part about the "auto_complete" method:
def auto_complete_for_person_name
  @people = Person.find(:all,
    :conditions => ["name LIKE ? AND org = ?", "#{params[:person]
[:name]}%", params[:person][:org]],
    :order => "name ASC")
  unless @people.blank?
    render :inline => "<%= content_tag(:ul,
      @people.map{|person|
        content_tag(:li, h(person)) }) %>"
  else
    render :inline => ""
  end
end

Remember I'm using Rails ver 1.2.6 - so it may not work for your
version.

Good Luck!

Peter

Couple of things
a) Your stylesheet is called complete.css but your stylesheet_link_tag
says "example" ?
b) Check your logs as you type to see if rails fires off queries
depending on your input.. The autocomplete method is doing a "find"
which should be firing off SQLs in ur log file. You would atleast know
if that part of it is working.

hello guys!!!

it's me again, I still have problem with my auto_complete, but finally
I got an error, these is what firebug says

<html xmlns="http://www.w3.org/1999/xhtml">
<head>

  <title>Action Controller: Exception caught</title>

  <style>

    body { background-color: #fff; color: #333; }
    body, p, ol, ul, td {

      font-family: verdana, arial, helvetica, sans-serif;

      font-size: 13px;

      line-height: 18px;

    }

    pre {

      background-color: #eee;

      padding: 10px;

      font-size: 11px;

    }

    a { color: #000; }

Finally works!!!!!!! these is all I did
1.controller
auto_complete_for :alumno, :nombre
       protect_from_forgery :only => [:create, :delete, :update]
2.in my view
<%= text_field_with_auto_complete :alumno, :nombre, {}, {:method =>
:get} %>
3.in my route
map.resources :alumnos, :collection => {:auto_complete_for_alumno_nombre
=> :get }

and that's it. Thanks to all.....