# Text\_field\_with\_auto\_complete

**URL:** <https://www.ruby-forum.com/t/text-field-with-auto-complete/174145>\
**Category:** Rails\
**Created:** [August 21, 2009, 11:12am UTC](https://www.ruby-forum.com/t/text-field-with-auto-complete/174145 "2009-08-21T11:12:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pmckinstry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f6c823/32.png) [@pmckinstry](https://www.ruby-forum.com/u/pmckinstry)\
**Post date:** [August 21, 2009, 11:12am UTC](https://www.ruby-forum.com/t/text-field-with-auto-complete/174145/1 "2009-08-21T11:12:28Z")

</div>

I feel I’m on a constant Yak Shave  
([http://projects.csail.mit.edu/gsb/old-archive/gsb-archive/gsb2000-02-11.html](http://projects.csail.mit.edu/gsb/old-archive/gsb-archive/gsb2000-02-11.html))

I am playing with Auto Complete’s, due to some excellent tutorials on  
[railscast.com](http://railscast.com) I have been making progress although hit a small stumbling  
block which hope you can solve.

So I am auto completing from my Country model, which has 2 attributes  
‘name’ and ‘code’

If I just wanted the ‘name’ attribute to be returned then this would  
suffice in my returns js

\<%= auto\_complete\_result @countries, :country %\>

However what I actually want is ‘Country [code]’ e.g ‘Great Britain [ GB  
]’

However I don’t seem to be able to get this to work - in my Country  
model I have added something like this.

def code\_and\_country  
“#{name} [#{code}]”  
end

def code\_and\_country=(code\_and\_name)  
if m = code\_and\_name.match(/^([^[]+)[\s(\w{2})\s]/)  
code = m[2]  
name = m[1]  
end  
end

so had hoped that I could use

\<%= auto\_complete\_result @countries, :code\_and\_country %\> - but I think  
I am getting muddled up with methods and attributes.

So could someone be able to guide me on the correct way of doing it,  
e.g. making code\_and\_country a fully fledged ‘attribute’ of Country or  
somehow modifying the auto\_complete\_result helper to do the right thing?

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![pmckinstry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f6c823/32.png) [@pmckinstry](https://www.ruby-forum.com/u/pmckinstry)\
**Post date:** [August 21, 2009, 11:14am UTC](https://www.ruby-forum.com/t/text-field-with-auto-complete/174145/2 "2009-08-21T11:14:12Z")

</div>

Sorry made a typo its

\<%= auto\_complete\_result @countries, :name %\>

not

\<%= auto\_complete\_result @countries, :country %\>

as :country is not valid!

---

<div class="post-metadata">

**Author:** ![pmckinstry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f6c823/32.png) [@pmckinstry](https://www.ruby-forum.com/u/pmckinstry)\
**Post date:** [August 21, 2009, 12:12pm UTC](https://www.ruby-forum.com/t/text-field-with-auto-complete/174145/3 "2009-08-21T12:12:52Z")

</div>

To answer my own question - it seems text\_field\_with\_auto\_complete takes  
a array of hashes, so returning

@countries = Country.search(params[:search]).map{|a| {‘code’ =\>  
a.code\_and\_country}}

\<%= auto\_complete\_result(@countries, ‘code’) %\>
