Problem with auto_complete!

Hello!!!

first of all sorry for my English! :-(

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)

Mi boleta en la WEB <%= 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 %>

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

  1. 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)

    <% for nombre in @nombres .to_a -%>
  • <%= 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:

Please guys I need help with the auto_complete pluging, some ideas!!!
:frowning:

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:

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

Greg DeVore

On Oct 1, 8:53 am, Juan M. [email protected]

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:

#102 Auto-Complete Association - RailsCasts

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/

On Oct 1, 6:04 pm, Juan M. [email protected]

Juan M. wrote:

Hello!!!

first of all sorry for my English! :-(

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)

Mi boleta en la WEB <%= 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 %>

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

  1. 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)

    <% for nombre in @nombres .to_a -%>
  • <%= 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

Action Controller: Exception caught 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; } a:visited { color: #666; } a:hover { color: #fff; background-color:#000; }

ActionController::InvalidAuthenticityToken

in PruebaController#auto_complete_for_alumno_nombre
ActionController::InvalidAuthenticityToken

RAILS_ROOT: /home/jpmg/ruby/completar

Request

Parameters:

{"alumno"=>{"nombre"=>"K"}}

<a href="#" onclick="document.getElementById('session_dump').style.display='block'; return false;"

Show session dump

---

:csrf_id: c55efda528b897a47f929b3d45f8b6aa

flash: !map:ActionController::Flash::FlashHash {}

Response

Headers:

{"cookie"=>[],

"Cache-Control"=>"no-cache"}

can someone help me please I really want to get it right..thank you!! :-)

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…