# Text\_field\_with\_auto\_complete help

**URL:** <https://www.ruby-forum.com/t/text-field-with-auto-complete-help/130998>\
**Category:** Rails\
**Created:** [March 5, 2008, 2:22am UTC](https://www.ruby-forum.com/t/text-field-with-auto-complete-help/130998 "2008-03-05T02:22:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dino\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@dino\_d](https://www.ruby-forum.com/u/dino_d)\
**Post date:** [March 5, 2008, 2:22am UTC](https://www.ruby-forum.com/t/text-field-with-auto-complete-help/130998/1 "2008-03-05T02:22:02Z")

</div>

Hi-

I am trying to understand how auto\_complete works. The docs don’t  
tell me what each parameter is, they all assume I know what they are.  
All I want to do is have a form that contains news stories that you  
can tag, with the tag suggestions coming from the tags table.

In one example ( [http://agilewebdevelopment.com/plugins/auto\_complete](http://agilewebdevelopment.com/plugins/auto_complete)  
) he says:

> # Controller
> 
> class BlogController \< ApplicationController  
> auto\_complete\_for :post, :title  
> end
> 
> # View
> 
> \<%= text\_field\_with\_auto\_complete :post, title %\>

Here, you have the controller class, Blog, and post, and title. What  
are these? Is post the name of an existing object? Is title the  
field of a class?

In another example (  
[http://trix.pl/blog/ruby-on-rails/auto-complete-for-rails-2-0-tutorial](http://trix.pl/blog/ruby-on-rails/auto-complete-for-rails-2-0-tutorial)  
) , he says:

> class DomainsController \< ApplicationController
> 
> auto\_complete\_for :domain, :name

here, the controller name is the same as the first param of the  
method.

It’s all so confusing.

Anyway, can someone break down what the parameters mean?

My situation specifically is, I have a stories table, a storytags  
table and a tags table. I want the user to be able to add a tag to a  
story on a form, using autocomplete for a field in a table that’s not  
the same as the primary table (stories).

If anyone can give me a push in the right direction, I’d appreciate  
it.

Thanks,  
Dino

---

<div class="post-metadata">

**Author:** ![dino\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@dino\_d](https://www.ruby-forum.com/u/dino_d)\
**Post date:** [March 5, 2008, 4:04am UTC](https://www.ruby-forum.com/t/text-field-with-auto-complete-help/130998/2 "2008-03-05T04:04:44Z")

</div>

Hi,

# Extract from APRESS book…pg 218

Listing 7-16.  
Adding the auto\_complete\_for helper in app/controllers/  
events\_controller.rb

class EventsController \< ApplicationController  
before\_filter :authenticate, :except =\> [:index, :show]  
auto\_complete\_for :event, :location  
#…  
end

This piece of code will tell the controller to return a list of the  
existing locations in the  
database.

By default, auto\_complete\_for limits the number of returned records to  
10 and orders  
the query by the field being searched. As always, those are sensible  
defaults; however, if  
you need to overwrite them, just specify them in the hash passed to  
auto\_complete\_for, as  
follows:  
auto\_complete\_for :event, :location, :limit =\> 25, :order =\> ‘title  
DESC’

On the template side, we need to make an adjustment to how we’re  
dealing with  
the location field. Fortunately, this is easy.

Simply replace the following lines in app/  
views/events/\_form.rhtml:

Location  
\<%= text\_field 'event', 'location' %\>

with the lines shown in Listing 7-17.

Listing 7-17. Updating app/views/events/\_form to Use Auto-Completion

Location  
\<%= text\_field\_with\_auto\_complete :event, :location %\>

The code is slim, and the result is quite pleasing. As you can see in  
Figure 7-7, as soon  
as you start to type the location, the auto\_complete\_for helper runs a  
database search to  
find existing values for the Location field that match what you’re  
typing. If you see the  
location you want to enter, you can select it (with the mouse or  
keyboard). If you don’t  
see the location, you can ignore the suggested values and keep typing.

===============================================================  
On Mar 5, 9:21 am, “dino d.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > # Controller
> 
> It’s all so confusing.
> 
> Anyway, can someone break down what the parameters mean?

---

<div class="post-metadata">

**Author:** ![dino\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@dino\_d](https://www.ruby-forum.com/u/dino_d)\
**Post date:** [March 5, 2008, 5:28am UTC](https://www.ruby-forum.com/t/text-field-with-auto-complete-help/130998/3 "2008-03-05T05:28:39Z")

</div>

> Extract from APRESS book…pg 218

Thanks for the reply. What do you do if the source of the  
auto\_complete is not a field in the model? All of the examples I have  
found assume this.

> This piece of code will tell the controller to return a list of the  
> existing locations in the  
> database.

Again, yes, assuming locations is not a has\_many table, or, eve worse,  
a has\_and\_blongs\_to\_many. Any ideas in these cases?

Thanks again,  
Dino
