# Text\_field instead of a collection\_select

**URL:** <https://www.ruby-forum.com/t/text-field-instead-of-a-collection-select/182533>\
**Category:** Rails\
**Created:** [January 29, 2010, 1:21pm UTC](https://www.ruby-forum.com/t/text-field-instead-of-a-collection-select/182533 "2010-01-29T13:21:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![eugenio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ec9cab/32.png) [@eugenio](https://www.ruby-forum.com/u/eugenio)\
**Post date:** [January 29, 2010, 1:21pm UTC](https://www.ruby-forum.com/t/text-field-instead-of-a-collection-select/182533/1 "2010-01-29T13:21:27Z")

</div>

one of my model belongs\_to another (zip codes).  
i would like to let the user insert the zip code in a text field  
instead of the select menu generated by the collection\_select.  
No new zip code should be created and the code should be validated (it  
should be in the list of valid code).  
How can this be achieved?

---

<div class="post-metadata">

**Author:** ![eugenio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ec9cab/32.png) [@eugenio](https://www.ruby-forum.com/u/eugenio)\
**Post date:** [January 29, 2010, 10:35pm UTC](https://www.ruby-forum.com/t/text-field-instead-of-a-collection-select/182533/2 "2010-01-29T22:35:48Z")

</div>

> one of my model belongs\_to another (zip codes).  
> i would like to let the user insert the zip code in a text field  
> instead of the select menu generated by the collection\_select.  
> No new zip code should be created and the code should be validated (it  
> should be in the list of valid code).  
> How can this be achieved?

You want an auto completing text field. There are some plugins out  
there that can do this and associate it to the other model  
automatically. Here’s one:

[http://model-ac.rubyforge.org/svn/trunk/vendor/plugins/model\_auto\_completer](http://model-ac.rubyforge.org/svn/trunk/vendor/plugins/model_auto_completer)

You can also roll your own using a prototype/jquery auto completing  
plugin…

-philip

---

<div class="post-metadata">

**Author:** ![eugenio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ec9cab/32.png) [@eugenio](https://www.ruby-forum.com/u/eugenio)\
**Post date:** [January 30, 2010, 9:59am UTC](https://www.ruby-forum.com/t/text-field-instead-of-a-collection-select/182533/3 "2010-01-30T09:59:52Z")

</div>

You might want to check out:

[http://github.com/Erol/nested\_reference](http://github.com/Erol/nested_reference)

Assuming your models were Person and ZipCode, and ZipCode has a field  
called ‘code’:

class Person  
belongs\_to :zip\_code  
validates\_presence\_of :zip\_code, :message =\> ‘Zip Code can’t be  
empty or was not found in the list of valid codes.’

accepts\_nested\_reference\_and\_attributes\_for :zip\_code, :code, :required  
=\> true  
end

View:

form\_for @person do |p|  
p.text\_field :name  
p.fields\_for :zip\_code |zc|  
zc.text\_field :code  
end  
end

Controller:

person.update\_attributes(:person) # Nothing special needs to be done.  
person.zip\_code\_id will be set depending on the zip code the user  
filled in.

You can add auto-completion in your view. I’d recommend jQuery  
Autocomplete.

Hope this helps,

---

<div class="post-metadata">

**Author:** ![eugenio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ec9cab/32.png) [@eugenio](https://www.ruby-forum.com/u/eugenio)\
**Post date:** [January 30, 2010, 10:37am UTC](https://www.ruby-forum.com/t/text-field-instead-of-a-collection-select/182533/4 "2010-01-30T10:37:19Z")

</div>

Sorry for the garbled code. Here’s a pastie link instead:

[http://www.pastie.org/801584](http://www.pastie.org/801584)

---

<div class="post-metadata">

**Author:** ![eugenio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ec9cab/32.png) [@eugenio](https://www.ruby-forum.com/u/eugenio)\
**Post date:** [February 10, 2010, 11:52am UTC](https://www.ruby-forum.com/t/text-field-instead-of-a-collection-select/182533/5 "2010-02-10T11:52:28Z")

</div>

i tried the plugin you suggested, which seems exactly what i’m looking  
for.  
but i got stuck:

my user model has this line  
belongs\_to :cap\_residenza, :class\_name =\> ‘Cap’  
and i added

accepts\_nested\_reference\_and\_attributes\_for :cap\_residenza, :codice,  
:required  
=\> true

in the form view i added this:  
\<% f.fields\_for :cap\_residenza do |fr| %\>  
\<%= fr.label :cap %\>  
\<%= fr.text\_field :codice %\>  
\<% end %\>  
but nothing appears. if i change ‘cap\_residenza’ with anything else  
the form shows up correctly (but it doesn’t work, everything else is  
looking for cap\_residenza i suppose).  
what am i missing?

thank you

---

<div class="post-metadata">

**Author:** ![eugenio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ec9cab/32.png) [@eugenio](https://www.ruby-forum.com/u/eugenio)\
**Post date:** [February 10, 2010, 12:53pm UTC](https://www.ruby-forum.com/t/text-field-instead-of-a-collection-select/182533/6 "2010-02-10T12:53:55Z")

</div>

On Feb 10, 6:50 pm, eugenio [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> thank you  
> Does it work if you remove the  
> accepts\_nested\_reference\_and\_attributes\_for line? If no, could you  
> send the models and form snippets (with the form\_for block) as a  
> pastie?
