Text_field_with_auto_complete help

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
) 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
) , 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

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.” [email protected] wrote:

Controller

It’s all so confusing.

Anyway, can someone break down what the parameters mean?

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