# Auto\_complete\_for not reading from the database?

**URL:** <https://www.ruby-forum.com/t/auto-complete-for-not-reading-from-the-database/72563>\
**Category:** Rails\
**Created:** [October 18, 2006, 2:55am UTC](https://www.ruby-forum.com/t/auto-complete-for-not-reading-from-the-database/72563 "2006-10-18T02:55:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jacquie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jacquie](https://www.ruby-forum.com/u/jacquie)\
**Post date:** [October 18, 2006, 2:55am UTC](https://www.ruby-forum.com/t/auto-complete-for-not-reading-from-the-database/72563/1 "2006-10-18T02:55:23Z")

</div>

I know I am going to realize how blonde this question is but at the  
moment I was stuck for long.

I tried to use the sample autocomplete code from  
[http://demo.script.aculo.us/ajax/autocompleter\_customized](http://demo.script.aculo.us/ajax/autocompleter_customized)

and just change the code a bit to fit into my application. But everytime  
I insert something,it looks like the application is not reading from the  
existing database.I am sure there is something in the datatbase. I  
checkd the log, it says  
127.0.0.1 - - [18/Oct/2006:10:47:42 AUS Eastern Standard Time] “POST  
/admin/cont  
ent/auto\_complete\_for\_place\_location HTTP/1.1” 500 9164

- → /admin/content/auto\_complete\_for\_place\_location  
127.0.0.1 - - [18/Oct/2006:10:47:43 AUS Eastern Standard Time] “POST  
/admin/cont  
ent/preview HTTP/1.1” 200 12
- → /admin/content/preview

The code is shown as below.  
View : app/views/admin/content/\_form.rhtml

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

Controller: app/controller/admin/content\_controller.rb

def auto\_complete\_for\_place\_location  
@places = Place.find(:all, :conditions =\> [ ‘LOWER(location) LIKE  
?’,‘%’ + params[:place][:location].downcase + ‘%’ ],:order =\> ‘location  
ASC’,:limit =\> 8)  
render :partial =\> ‘search’  
end

render Partial: app/views/admin/content/\_search.rhtml

\<% for place in @places do -%\> - 
 ![](https://www.ruby-forum.com/images/places/<%=%20place.id.to_s[-1,1]%>.jpg)

\<%=h places.location %\>

\<%=h places.state %\>
\<% end -%\>

The database is places with column location. I tried all possibilities  
and nothing seems to work. I can see that other people use it with no  
problem.

Can anyone who has used it give me some advice as to where the problem  
could be?  
Thank you very much in advance!!

---

<div class="post-metadata">

**Author:** ![jacquie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jacquie](https://www.ruby-forum.com/u/jacquie)\
**Post date:** [October 18, 2006, 4:55am UTC](https://www.ruby-forum.com/t/auto-complete-for-not-reading-from-the-database/72563/2 "2006-10-18T04:55:24Z")

</div>

> Can anyone who has used it give me some advice as to where the problem  
> could be?  
> Thank you very much in advance!!

One thing they leave out from the sample is the CSS, you need to add  
this CSS to your layout…  
(Change the class names to suit).

div.auto\_complete {  
position:absolute;  
width:250px;  
background-color:white;  
border:1px solid #888;  
margin:0px;  
padding:0px;  
}  
ul.auto\_complete {  
list-style-type: none;  
margin:0px;  
padding:0px;  
}  
ul.auto\_complete li.selected { background-color: #ffb; }  
li.place{  
list-style-type: none;  
display:block;  
margin:0;  
padding:2px;  
height:32px;  
}

li.place div.name {  
font-weight:bold;  
font-size:12px;  
line-height:1.2em;  
}  
li.place div.location {  
font-size:10px;  
color:#888;  
}

also in your code you need to change places to place

> ```
> <div class="location"><%=h places.location %></div>
> <div class="location"><%=h places.state %></div>
> 
> ```

places here should be place. (You may want a different class name on  
each of these)

and lastly if you see a large amount of space in the autocomplete box  
after selecting something you  
need to add the name of the class you want to insert in the text box  
after selecting…

\<%= text\_field\_with\_auto\_complete :place, :location, {},  
{:select =\> ‘location’, :skip\_style =\> true} %\>

See if that helps.

Jacquie F. wrote:

> 127.0.0.1 - - [18/Oct/2006:10:47:42 AUS Eastern Standard Time] "POST  
> View : app/views/admin/content/\_form.rhtml  
> render :partial =\> ‘search’
> 
> \<%=h places.location %\>
>   
> 
> \<%=h places.state %\>
> 
> \<% end -%\>
> 
> The database is places with column location. I tried all possibilities  
> and nothing seems to work. I can see that other people use it with no  
> problem.

–  
Jim M., [http://blog.wolfman.com](http://blog.wolfman.com)

---

<div class="post-metadata">

**Author:** ![jacquie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jacquie](https://www.ruby-forum.com/u/jacquie)\
**Post date:** [October 18, 2006, 7:13am UTC](https://www.ruby-forum.com/t/auto-complete-for-not-reading-from-the-database/72563/3 "2006-10-18T07:13:52Z")

</div>

Oops! Now I know how dumb my question is. its working like magic now.  
and I also added

#list li {  
margin:0;  
margin-bottom:4px;  
padding:5px;  
border:1px solid #888;  
cursor:move;  
}

to get the cursor selecting from those options.

Thanks very much Jim. saved me heaps of time.

Thank you again!!! =D

---

<div class="post-metadata">

**Author:** ![jacquie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jacquie](https://www.ruby-forum.com/u/jacquie)\
**Post date:** [October 18, 2006, 8:07am UTC](https://www.ruby-forum.com/t/auto-complete-for-not-reading-from-the-database/72563/4 "2006-10-18T08:07:34Z")

</div>

Jacquie F. wrote:

> to get the cursor selecting from those options.
> 
> Thanks very much Jim. saved me heaps of time.
> 
> Thank you again!!! =D

No Problem, it was not a dumb question, I had just spent 4 hours getting  
that to work properly, with  
custom drop downs, in addition to getting multiple autocomplete fields  
on the same page to work with  
indexes 🙂

–  
Jim M., [http://blog.wolfman.com](http://blog.wolfman.com)
