# AJAX autocomplete based on a static table in the database

**URL:** <https://www.ruby-forum.com/t/ajax-autocomplete-based-on-a-static-table-in-the-database/69660>\
**Category:** Rails\
**Created:** [September 22, 2006, 7:34pm UTC](https://www.ruby-forum.com/t/ajax-autocomplete-based-on-a-static-table-in-the-database/69660 "2006-09-22T19:34:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aeneas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d07c76/32.png) [@Aeneas](https://www.ruby-forum.com/u/Aeneas)\
**Post date:** [September 22, 2006, 7:34pm UTC](https://www.ruby-forum.com/t/ajax-autocomplete-based-on-a-static-table-in-the-database/69660/1 "2006-09-22T19:34:09Z")

</div>

I have a field where a user enters his city. I want to add  
autocompletion to this field based on a table with a predefined set of  
cities not based on previous entries of other users. How can I do this?

I know about the method described in rails recipes which allows to  
cache a predefined set of values for autocompletion  
(Autocomplete.Local). This is not what I’m looking for though because  
my list of cities is very long and I don’t want to cache it.

Thanks,  
Aeneas

---

<div class="post-metadata">

**Author:** ![Aeneas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d07c76/32.png) [@Aeneas](https://www.ruby-forum.com/u/Aeneas)\
**Post date:** [September 22, 2006, 7:56pm UTC](https://www.ruby-forum.com/t/ajax-autocomplete-based-on-a-static-table-in-the-database/69660/2 "2006-09-22T19:56:14Z")

</div>

On 9/22/06, Aeneas [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have a field where a user enters his city. I want to add  
> autocompletion to this field based on a table with a predefined set of  
> cities not based on previous entries of other users. How can I do this?
> 
> I know about the method described in rails recipes which allows to  
> cache a predefined set of values for autocompletion  
> (Autocomplete.Local). This is not what I’m looking for though because  
> my list of cities is very long and I don’t want to cache it.

You can write your own “auto\_complete\_for\_#{object}\_#{method}” method  
pretty easily, rather than calling auto\_complete\_for to generate it  
for you. The source code of auto\_complete\_for is a good starting  
point.

– James

---

<div class="post-metadata">

**Author:** ![Aeneas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d07c76/32.png) [@Aeneas](https://www.ruby-forum.com/u/Aeneas)\
**Post date:** [September 22, 2006, 9:22pm UTC](https://www.ruby-forum.com/t/ajax-autocomplete-based-on-a-static-table-in-the-database/69660/3 "2006-09-22T21:22:21Z")

</div>

Thanks James. I solved the problem now. It’s really easy. In your  
controller you just replace

auto\_complete\_for :ad, :city

with

def auto\_complete\_for\_ad\_city  
@cities = City.find(:all)  
render :inline =\> “\<%= auto\_complete\_result(@cities, ‘name’) %\>”  
end

For this to work you will need a table called cities and a city model.

Aeneas

---

<div class="post-metadata">

**Author:** ![Aeneas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d07c76/32.png) [@Aeneas](https://www.ruby-forum.com/u/Aeneas)\
**Post date:** [September 22, 2006, 11:12pm UTC](https://www.ruby-forum.com/t/ajax-autocomplete-based-on-a-static-table-in-the-database/69660/4 "2006-09-22T23:12:31Z")

</div>

I still have one probelm though. The soruce for auto\_complete\_for is as  
follows:

37: def auto\_complete\_for(object, method, options = {})  
38: define\_method(“auto\_complete\_for\_#{object}\_#{method}”) do  
39: find\_options = {  
40: :conditions =\> [ “LOWER(#{method}) LIKE ?”, ‘%’ +  
params[object][method].downcase + ‘%’ ],  
41: :order =\> “#{method} ASC”,  
42: :limit =\> 10 }.merge!(options)  
43:  
44: @items = object.to\_s.camelize.constantize.find(:all,  
find\_options)  
45:  
46: render :inline =\> “\<%= auto\_complete\_result @items,  
‘#{method}’ %\>”  
47: end  
48: end

However, the following adaption dowsn’t work (in the controller):

def auto\_complete\_for\_ad\_city  
@items = City.to\_s.camelize.constantize.find( :all,  
:conditions =\> [ “LOWER(name) LIKE ?”, ‘%’ +  
params[ad][city].downcase + ‘%’ ],  
:order =\> “name ASC”,  
:limit =\> 10  
)  
render :inline =\> “\<%= auto\_complete\_result(@items, ‘name’)  
%\>”  
end

the part causing problems is params[city][name]. Is this really the  
variable which stores the letters typed into the field ad[city]? The  
error message is: undefined local variable or method `ad’ for  
#AdsController:0xb79908c0

Any ideas?

Aeneas

---

<div class="post-metadata">

**Author:** ![Aeneas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d07c76/32.png) [@Aeneas](https://www.ruby-forum.com/u/Aeneas)\
**Post date:** [September 22, 2006, 11:20pm UTC](https://www.ruby-forum.com/t/ajax-autocomplete-based-on-a-static-table-in-the-database/69660/5 "2006-09-22T23:20:32Z")

</div>

I still have one probelm though. The soruce for auto\_complete\_for is as  
follows:

37: def auto\_complete\_for(object, method, options = {})  
38: define\_method(“auto\_complete\_for\_#{object}\_#{method}”) do  
39: find\_options = {  
40: :conditions =\> [ “LOWER(#{method}) LIKE ?”, ‘%’ +  
params[object][method].downcase + ‘%’ ],  
41: :order =\> “#{method} ASC”,  
42: :limit =\> 10 }.merge!(options)  
43:  
44: @items = object.to\_s.camelize.constantize.find(:all,  
find\_options)  
45:  
46: render :inline =\> “\<%= auto\_complete\_result @items,  
‘#{method}’ %\>”  
47: end  
48: end

However, the following adaption dowsn’t work (in the controller):

def auto\_complete\_for\_ad\_city  
@items = City.to\_s.camelize.constantize.find( :all,  
:conditions =\> [ “LOWER(name) LIKE ?”, ‘%’ +  
params[ad][city].downcase + ‘%’ ],  
:order =\> “name ASC”,  
:limit =\> 10  
)  
render :inline =\> “\<%= auto\_complete\_result(@items, ‘name’)  
%\>”  
end

the part causing problems is params[city][name]. Is this really the  
variable which stores the letters typed into the field ad[city]? The  
error message is: undefined local variable or method `ad’ for  
#AdsController:0xb79908c0

Any ideas?

Aeneas

---

<div class="post-metadata">

**Author:** ![Aeneas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d07c76/32.png) [@Aeneas](https://www.ruby-forum.com/u/Aeneas)\
**Post date:** [September 23, 2006, 12:26am UTC](https://www.ruby-forum.com/t/ajax-autocomplete-based-on-a-static-table-in-the-database/69660/6 "2006-09-23T00:26:38Z")

</div>

JDL wrote:

> > 41: :order =\> “#{method} ASC”,  
> > However, the following adaption dowsn’t work (in the controller):  
> > end  
> > params[:ad][:city]
> 
> If that’s not where your trouble lies, look at your development log  
> file. Every param is logged there, so you should be able to see the  
> names being used.
> 
> – James

Thanks, that was it!

So for the archive here the working version:

def auto\_complete\_for\_ad\_city  
@items = City.to\_s.camelize.constantize.find( :all,  
:conditions =\> [ “LOWER(name) LIKE ?”, ‘%’ +  
params[:ad][:city].downcase + ‘%’ ],  
:order =\> “name ASC”,  
:limit =\> 10  
)  
render :inline =\> “\<%= auto\_complete\_result(@items, ‘name’) %\>”  
end

---

<div class="post-metadata">

**Author:** ![Aeneas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d07c76/32.png) [@Aeneas](https://www.ruby-forum.com/u/Aeneas)\
**Post date:** [September 22, 2006, 11:26pm UTC](https://www.ruby-forum.com/t/ajax-autocomplete-based-on-a-static-table-in-the-database/69660/7 "2006-09-22T23:26:32Z")

</div>

On 9/22/06, Aeneas [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 42: :limit =\> 10 }.merge!(options)
> 
> the part causing problems is params[city][name]. Is this really the  
> variable which stores the letters typed into the field ad[city]? The  
> error message is: undefined local variable or method `ad’ for  
> #AdsController:0xb79908c0
> 
> Any ideas?

Did you really mean this instead?

params[:ad][:city]

If that’s not where your trouble lies, look at your development log  
file. Every param is logged there, so you should be able to see the  
names being used.

– James
