# Customized auto complete with preloaded array

**URL:** <https://www.ruby-forum.com/t/customized-auto-complete-with-preloaded-array/112317>\
**Category:** Rails\
**Created:** [August 17, 2007, 12:33pm UTC](https://www.ruby-forum.com/t/customized-auto-complete-with-preloaded-array/112317 "2007-08-17T12:33:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![martin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/73ab20/32.png) [@martin](https://www.ruby-forum.com/u/martin)\
**Post date:** [August 17, 2007, 12:33pm UTC](https://www.ruby-forum.com/t/customized-auto-complete-with-preloaded-array/112317/1 "2007-08-17T12:33:33Z")

</div>

Hi,

I want to have an autocomplete which displays the name and email  
address of the contact.  
But, the contact list array should be pre loaded, as described in  
Rails Recipes.  
Since this recipe doesn´t use any partials, I´m not sure how to  
accomplish this.

It displays number.name, but I’m not sure how to make it display  
number.email as well

search\_page.rhtml

```
<input type="text" id="number_lookup" name="number_lookup" /

<div class="auto_complete" id="number_lookup_auto_complete">
</div>
<%= javascript_tag("new Autocompleter.Local('number_lookup',
                               'number_lookup_auto_complete',
                               numbers,
                               {fullSearch: true,
                                  frequency: 0,
                                  minChars: 2
                                }
                                );") %>

```

controller:  
def numbers\_for\_lookup  
@numbers = Number.find(:all, :conditions =\> [“company =  
‘#{session[:user\_company]}’”])  
@headers[‘content-type’] = ‘text/javascript’  
render :layout =\> false  
end

numbers\_for\_lookup.rhtml:  
var numbers = new Array(\<%= @numbers.size %\>);

\<% @numbers.each\_with\_index do |number, index| %\>  
numbers[\<%= index %\>] = “\<%= number.name %\>”;  
\<% end %\>

Any suggestions to how I can display the email address as well in the  
result box?

Regards,  
Martin S.

---

<div class="post-metadata">

**Author:** ![martin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/73ab20/32.png) [@martin](https://www.ruby-forum.com/u/martin)\
**Post date:** [August 17, 2007, 4:15pm UTC](https://www.ruby-forum.com/t/customized-auto-complete-with-preloaded-array/112317/2 "2007-08-17T16:15:57Z")

</div>

Actually, you can use a partial. For an autocomplete list the  
partial might be named something like \_autocomplete.rhtml and would  
contain something like the following:

 \<% @numbers.each do | number | %\> - \<%= number.number %\> \<%= number.email %\>
 \<% end %\>

I don’t have AWDR2 in front of me, but I believe it’s just a  
render :partial =\> ‘autocomplete’, :layout =\> false

from inside the controller.

paul
