# Plugin auto\_complete - unable to get it to work at all

**URL:** <https://www.ruby-forum.com/t/plugin-auto-complete-unable-to-get-it-to-work-at-all/172618>\
**Category:** Rails\
**Created:** [July 24, 2009, 5:47am UTC](https://www.ruby-forum.com/t/plugin-auto-complete-unable-to-get-it-to-work-at-all/172618 "2009-07-24T05:47:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SSSSS\_lphSSSSBlSSS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9a140/32.png) [@SSSSS\_lphSSSSBlSSS\_S](https://www.ruby-forum.com/u/SSSSS_lphSSSSBlSSS_S)\
**Post date:** [July 24, 2009, 5:47am UTC](https://www.ruby-forum.com/t/plugin-auto-complete-unable-to-get-it-to-work-at-all/172618/1 "2009-07-24T05:47:28Z")

</div>

I’m having a little trouble trying to understand how autocomplete works.  
This is namely because there are so many plugins out there and some are  
outdated and many come with mixed instructions.

I’m using the standard plugin called auto\_complete from David Heinemeier  
Hansson.

Here’s what I have so far:

Teams controller

auto\_complete\_for :team, :name

def virtual\_matchups  
@teams = Team.find(:all, :conditions =\> [‘name LIKE ?’,  
“%#{params[:search]}%”])  
end

Created a custom method called virtual\_matchups. The name of the model  
is Team and the name of the team name field is name.

I created a custom view:

virtual\_matchups.html.erb

# Test Virtual Matchups
\<%= text\_field\_with\_auto\_complete :team, :name %\>

I added the following to routes.rb:

map.auto\_complete ‘:controller/:action’, :requirements =\> { :action =\>  
/auto\_complete\_for\_\S+/ }, :conditions =\> { :method =\> :get }

…

I go to [http://localhost:3000/teams/virtual\_matchups](http://localhost:3000/teams/virtual_matchups)

… I get:

Couldn’t find Team with ID=virtual\_matchups

I go to [http://localhost:3000/teams/virtual\_matchups/1](http://localhost:3000/teams/virtual_matchups/1)

… I get:

the text field box but when typing into it, it does nothing…

I could use some help with a step by step procedure on how to use auto  
complete. If I can do it just one time, I’m sure I could figure it out.  
However, I’ve been messing around with the regular version, a jquery  
version, even the ryan bates railscast and “none of them” work…

However, all start right in the middle of something and never explain  
how to use it step by step from the beginning. I can’t find a solid  
tutorial on using the plugin.

## Thanks.

Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![SSSSS\_lphSSSSBlSSS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9a140/32.png) [@SSSSS\_lphSSSSBlSSS\_S](https://www.ruby-forum.com/u/SSSSS_lphSSSSBlSSS_S)\
**Post date:** [July 24, 2009, 6:59am UTC](https://www.ruby-forum.com/t/plugin-auto-complete-unable-to-get-it-to-work-at-all/172618/2 "2009-07-24T06:59:55Z")

</div>

I believe I found a partial answer:

In my teams controller I placed:

class TeamsController \< ApplicationController  
skip\_before\_filter :verify\_authenticity\_token, :only =\>  
[:auto\_complete\_for\_team\_name]

def auto\_complete\_for\_team\_name()  
team\_name = params[:team][:name]  
@teams = Team.find(:all, :conditions =\> [‘name LIKE ?’, “%” +  
team\_name + “%”])  
render :partial =\> ‘teamname’  
end

def virtual\_matchups  
end

In my virtual\_matchups.html.erb file I placed as a test:

# Test Virtual Matchups
\<%= text\_field\_with\_auto\_complete :team, :name ,{}, :skip\_style =\> false %\>

I created a partial for teamname:

 \<% for team in @teams do %\> - 
 \<%=h team.name %\> 
 \<% end %\>

In my routing file I placed:

map.virtual\_matchups ‘/virtual\_matchups’, :controller =\> ‘teams’,  
:action =\> ‘virtual\_matchups’

I restarted the server and the autocomplete works for this page, but I  
haven’t tried it with a form.

My thoughts on this are it was very cumbersome to get it to work. I had  
to go through several different sites, put together all the pieces of  
the puzzle before it finally worked.

I have no idea why I had to use the skip\_before\_filter but without it,  
it would not autocomplete.

Can someone please look over the fixes I provided and if you have  
knowledge of autocomplete let me know if there is a simpler way of  
getting it to work, and/or what I need to do to polish up the code I  
created thus far to get it to work?

I will need two fields and a submit button that performs a query to the  
same controller but to a different action that will render a completely  
different partial containing information.

Many thanks in advance.

–  
Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![SSSSS\_lphSSSSBlSSS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9a140/32.png) [@SSSSS\_lphSSSSBlSSS\_S](https://www.ruby-forum.com/u/SSSSS_lphSSSSBlSSS_S)\
**Post date:** [July 24, 2009, 7:46am UTC](https://www.ruby-forum.com/t/plugin-auto-complete-unable-to-get-it-to-work-at-all/172618/3 "2009-07-24T07:46:05Z")

</div>

my guess:  
auto\_complete\_for\_team\_name is called through ajax and the ajax call  
does not provide (bring along) any authenticity\_token…

On Jul 24, 6:59 am, “Älphä Blüë” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![SSSSS\_lphSSSSBlSSS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9a140/32.png) [@SSSSS\_lphSSSSBlSSS\_S](https://www.ruby-forum.com/u/SSSSS_lphSSSSBlSSS_S)\
**Post date:** [July 24, 2009, 3:02pm UTC](https://www.ruby-forum.com/t/plugin-auto-complete-unable-to-get-it-to-work-at-all/172618/4 "2009-07-24T15:02:16Z")

</div>

Thanks Clemens.

Well I will continue to use the skip\_before\_filter with the only  
statement against that particular method then. This seems the easiest  
implementation as my search results finds a lot more problems doing  
other things…

---

<div class="post-metadata">

**Author:** ![SSSSS\_lphSSSSBlSSS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9a140/32.png) [@SSSSS\_lphSSSSBlSSS\_S](https://www.ruby-forum.com/u/SSSSS_lphSSSSBlSSS_S)\
**Post date:** [July 24, 2009, 8:09am UTC](https://www.ruby-forum.com/t/plugin-auto-complete-unable-to-get-it-to-work-at-all/172618/5 "2009-07-24T08:09:27Z")

</div>

links which substantiate my guess 😉  
[http://ryandaigle.com/articles/2007/9/24/what-s-new-in-edge-rails-better-cross-site-request-forging-prevention](http://ryandaigle.com/articles/2007/9/24/what-s-new-in-edge-rails-better-cross-site-request-forging-prevention)  
[http://henrik.nyh.se/2008/05/rails-authenticity-token-with-jquery](http://henrik.nyh.se/2008/05/rails-authenticity-token-with-jquery)
