# Text\_field\_with\_auto\_complete problem

**URL:** <https://www.ruby-forum.com/t/text-field-with-auto-complete-problem/83323>\
**Category:** Rails\
**Created:** [January 25, 2007, 6:06am UTC](https://www.ruby-forum.com/t/text-field-with-auto-complete-problem/83323 "2007-01-25T06:06:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![darrenevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@darrenevans](https://www.ruby-forum.com/u/darrenevans)\
**Post date:** [January 25, 2007, 6:06am UTC](https://www.ruby-forum.com/t/text-field-with-auto-complete-problem/83323/1 "2007-01-25T06:06:29Z")

</div>

I have some difficulties with text\_field\_with\_auto\_complete. Iâ€™m new to  
this, it just will not work, I must be doing something wrong. It would  
be great if someone could advise me. I have tried to follow advice  
available on the internet, so I donâ€™t know what the problem is. The  
script gets as far as the breakpoint in the controller, but it doesnâ€™t  
like the next line â€“ giving the error message â€œundefined method or local  
variable searchâ€. This is what I haveâ€¦

I have downloaded the .js files from aculo and followed their  
instructions. I have put the .js files in a folder called â€œJavascriptsâ€  
in the root of my application.

My table is called institutions and the field is called name

I have added the following to the head of my layout:

\<%= javascript\_include\_tag :defaults %\>

My view is called place and is the following:

| \<%= text\_field\_with\_auto\_complete :institution, :names, { :size =\> 15 }, :skip\_style =\> true -%\> |

In the controller I have:

def place  
end

def auto\_complete\_for\_institution\_names  
search = params[:institution][:names]  
breakpoint  
@institutions = Institution.search(search) unless search.blank?  
render :partial =\> “live/search”  
end

Then I have a partial called â€œ\_live.rhtmlâ€, which contains:

 \<% for institution in @institutions.to\_a -%\> - \<%= institution\_link institution.name, institution %\>
 \<% end -%\>

---

<div class="post-metadata">

**Author:** ![darrenevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@darrenevans](https://www.ruby-forum.com/u/darrenevans)\
**Post date:** [January 25, 2007, 4:22pm UTC](https://www.ruby-forum.com/t/text-field-with-auto-complete-problem/83323/2 "2007-01-25T16:22:36Z")

</div>

Hi

I still haven’t managed to resolve this issue, is this in some way  
related to the rails helpers or something???

Darren

---

<div class="post-metadata">

**Author:** ![darrenevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@darrenevans](https://www.ruby-forum.com/u/darrenevans)\
**Post date:** [January 25, 2007, 6:11pm UTC](https://www.ruby-forum.com/t/text-field-with-auto-complete-problem/83323/3 "2007-01-25T18:11:17Z")

</div>

i just did this too. (Rails version 1.1.6)  
i used the advance version  
[http://demo.script.aculo.us/ajax/autocompleter\_customized](http://demo.script.aculo.us/ajax/autocompleter_customized) (basic  
[http://demo.script.aculo.us/ajax/autocompleter](http://demo.script.aculo.us/ajax/autocompleter))  
i have an item that i want to associate to a project.

in the head of my layout I only call  
\<%= javascript\_include\_tag :defaults %\>  
I did not create a javascript folder

## view

\<%= start\_form\_tag :action =\> ‘associate\_to\_a\_project’, :id =\> @item %\>  
\<%= text\_field\_with\_auto\_complete :item, :project\_id, {} %\>

```
<%= submit_tag 'add' %>

```

## \<%= end\_form\_tag %\>

## controller

def associate\_to\_a\_project  
@item = Item.find(params[:id])  
@item.project =  
Project.find\_or\_create\_by\_name(params[:item][“project\_id”])  
if @item.update\_attributes(params[:item])  
flash[:notice] = ‘Project was successfully associated to item.’  
redirect\_to :action =\> ‘list’  
else  
render :action =\> ‘new’  
end  
end

def auto\_complete\_for\_item\_project\_id  
auto\_complete\_responder\_for\_projects params[:item][:project\_id]  
end

private  
def auto\_complete\_responder\_for\_projects(value)  
@projects = Project.find(:all,  
:conditions =\> [ ‘LOWER(name) LIKE ?’,  
‘%’ + value.downcase + ‘%’ ],  
:order =\> ‘name ASC’)  
render :partial =\> ‘projects’  
end

* * *

## view \_projects

\<% for project in @projects do -%\> - \<%=h project.name %\>
\<% end -%\>
------------------------------------------------------------------

and it worked. It did take me sometime to get scriptolicious’  
instructions to work for my app but it was just figuring out what  
variable to change/pass

hth  
John
