# Text field with auto complete to look up ID... again

**URL:** <https://www.ruby-forum.com/t/text-field-with-auto-complete-to-look-up-id-again/75189>\
**Category:** Rails\
**Created:** [November 8, 2006, 3:30am UTC](https://www.ruby-forum.com/t/text-field-with-auto-complete-to-look-up-id-again/75189 "2006-11-08T03:30:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![saizai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f4b2a3/32.png) [@saizai](https://www.ruby-forum.com/u/saizai)\
**Post date:** [November 8, 2006, 3:30am UTC](https://www.ruby-forum.com/t/text-field-with-auto-complete-to-look-up-id-again/75189/1 "2006-11-08T03:30:04Z")

</div>

I’m getting a bit frustrated with it.

I’d like to populate a hidden field with the ID so I don’t have to  
find\_by\_foo\_bar afterwards (and thus take yet another database hit [oy  
my poor database] and risk ambiguous fields) or uglify the dropdown with  
an ID number.

Now, this works:

person.org\_name =\> “org: person”

\_form.rhtml:

 \<%= text\_field\_with\_auto\_complete :person, :name %\> \<%= hidden\_field :association, :person\_id %\> \<--- the field I want populated

\_people.rhtml:

\<% for person in @people do -%\>- 
\<%= h person.org\_name %\>
\<% end -%\>

… but ONLY for clicks (and onmouseover). onselect, onkeypress, etc…  
do nothing, so it doesn’t work for tab or enter. And I can’t access the  
id from after\_update\_element 'cause the popup is no longer there to get  
queried and all I can access is the text field itself and the selected  
value (ie the result of person.org\_name rather than person.id). (Or can  
I? If so I don’t know how. :-/)

Any help?

Also, when I’m using autocomplete in a sub-sub-form the popup doesn’t  
show. 😕 When it’s just in a form or subform it does, though. And I can  
tell it’s getting called 'cause I see it on the log tail. I set  
position:absolute; in CSS; anything else that could do it?

- Sai

---

<div class="post-metadata">

**Author:** ![saizai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f4b2a3/32.png) [@saizai](https://www.ruby-forum.com/u/saizai)\
**Post date:** [November 9, 2006, 8:27am UTC](https://www.ruby-forum.com/t/text-field-with-auto-complete-to-look-up-id-again/75189/2 "2006-11-09T08:27:45Z")

</div>

I’m not sure it’ll answer all your questions, but check out my article  
on the subject here…

[http://blog.wolfman.com/articles/2006/10/23/getting-a-record-id-from-text\_field\_with\_auto\_complete](http://blog.wolfman.com/articles/2006/10/23/getting-a-record-id-from-text_field_with_auto_complete)

I hope it helps.

saizai wrote:

> \<% for person in @people do -%\>  
> id from after\_update\_element 'cause the popup is no longer there to get
> 
> - Sai

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

---

<div class="post-metadata">

**Author:** ![saizai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f4b2a3/32.png) [@saizai](https://www.ruby-forum.com/u/saizai)\
**Post date:** [November 9, 2006, 4:11pm UTC](https://www.ruby-forum.com/t/text-field-with-auto-complete-to-look-up-id-again/75189/3 "2006-11-09T16:11:39Z")

</div>

Jim M. wrote:

> I’m not sure it’ll answer all your questions, but check out my article  
> on the subject here…
> 
> [Wolfmans Howlings](http://blog.wolfman.com/articles/2006/10/23/getting-a-record-id-from-text_field_with_auto_complete)

That sounds like the text field will display “23,foo,bar”. Which is  
rather ugly. 😕

The page you linked to just does the same thing - makes the first field  
‘id’; hides it in the dropdown but it’ll be displayed as selected once  
something is chosen. Ugly UI. ☹ And then it uses that to populate other  
fields on the after-update call.

But I’d like a way to reliably populate a hidden field with the hidden  
‘id’ part of the popup no matter how selected, and have the text field  
just show the prettified name.

---

<div class="post-metadata">

**Author:** ![saizai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f4b2a3/32.png) [@saizai](https://www.ruby-forum.com/u/saizai)\
**Post date:** [November 11, 2006, 7:14am UTC](https://www.ruby-forum.com/t/text-field-with-auto-complete-to-look-up-id-again/75189/4 "2006-11-11T07:14:39Z")

</div>

Sai E. wrote:

> ‘id’; hides it in the dropdown but it’ll be displayed as selected once  
> something is chosen. Ugly UI. ☹ And then it uses that to populate other  
> fields on the after-update call.
> 
> But I’d like a way to reliably populate a hidden field with the hidden  
> ‘id’ part of the popup no matter how selected, and have the text field  
> just show the prettified name.

Ok that link does hide the id, which is not the link it was meant to  
point to 🙂

Here is where the link should have pointed to…

[http://wiki.rubyonrails.org/rails/pages/How+to+use+text\_field\_with\_auto\_complete/versions/67](http://wiki.rubyonrails.org/rails/pages/How+to+use+text_field_with_auto_complete/versions/67)  
about half way down, it has a nicer way to do it, but I have not tried  
that method.

There is another method here that uses javascript to write the id into a  
hidden\_text field, I have  
used that method.

[http://ricardo.pacheco.name/blog/articles/2006/09](http://ricardo.pacheco.name/blog/articles/2006/09)

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

---

<div class="post-metadata">

**Author:** ![saizai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f4b2a3/32.png) [@saizai](https://www.ruby-forum.com/u/saizai)\
**Post date:** [November 11, 2006, 7:22am UTC](https://www.ruby-forum.com/t/text-field-with-auto-complete-to-look-up-id-again/75189/5 "2006-11-11T07:22:40Z")

</div>

FYI I updated my blog with the correct links, and added two more ways of  
doing I found.

Jim M. wrote:

> > ‘id’; hides it in the dropdown but it’ll be displayed as selected once  
> > Here is where the link should have pointed to…
> 
> [http://wiki.rubyonrails.org/rails/pages/How+to+use+text\_field\_with\_auto\_complete/versions/67](http://wiki.rubyonrails.org/rails/pages/How+to+use+text_field_with_auto_complete/versions/67)  
> about half way down, it has a nicer way to do it, but I have not tried that method.
> 
> There is another method here that uses javascript to write the id into a hidden\_text field, I have  
> used that method.
> 
> [http://ricardo.pacheco.name/blog/articles/2006/09](http://ricardo.pacheco.name/blog/articles/2006/09)

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