# Text\_field\_with\_auto\_complete and record ID

**URL:** <https://www.ruby-forum.com/t/text-field-with-auto-complete-and-record-id/49500>\
**Category:** Rails\
**Created:** [December 5, 2005, 1:35pm UTC](https://www.ruby-forum.com/t/text-field-with-auto-complete-and-record-id/49500 "2005-12-05T13:35:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![liststuff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7bcc69/32.png) [@liststuff](https://www.ruby-forum.com/u/liststuff)\
**Post date:** [December 5, 2005, 1:35pm UTC](https://www.ruby-forum.com/t/text-field-with-auto-complete-and-record-id/49500/1 "2005-12-05T13:35:09Z")

</div>

I understand it’s not possible to retreive the ID of a record selected  
using  
text\_field\_with\_auto\_complete, but I’m struggling to create my own  
version  
of the helper with that functionaility.

has anyone managed to do this? if someone has a code sample, I’d be very  
grateful

---

<div class="post-metadata">

**Author:** ![liststuff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7bcc69/32.png) [@liststuff](https://www.ruby-forum.com/u/liststuff)\
**Post date:** [December 5, 2005, 7:24pm UTC](https://www.ruby-forum.com/t/text-field-with-auto-complete-and-record-id/49500/2 "2005-12-05T19:24:18Z")

</div>

Hello Alan !

2005/12/5, Alan B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I understand it’s not possible to retreive the ID of a record selected using  
> text\_field\_with\_auto\_complete, but I’m struggling to create my own version  
> of the helper with that functionaility.
> 
> has anyone managed to do this? if someone has a code sample, I’d be very  
> grateful

What I did is in the action that does the retrieval, I set the LI’s ID  
attribute to the record ID. Then, I have an afterUpdate callback that  
copies the ID in a hidden field.

Something like this:

app/views/products/\_form.rhtml:  
\<%= hidden\_field :product, :supplier\_id %\>  
\<%= text\_field :supplier, :name, :size =\> 40, :value =\>  
@product.supplier.nil? ? nil : @product.supplier.display\_name %\>  
  
\<%= link\_to ‘New supplier’, {:icon =\> ‘user\_add’, :controller =\>  
‘/admin/parties’, :action =\> :new}, {:target =\> ‘\_new’} %\>

app/views/products/auto\_complete\_for\_supplier\_name.rhtml:

\<%= render :partial =\> 'supplier', :collection =\> @suppliers %\>

app/views/products/\_supplier.rhtml:

- \<%= h(supplier.display\_name) %\>

This was built on Rails 0.12 originally, and hasn’t changed since. It  
should probably be revisited to use more helpers.

Bye !

---

<div class="post-metadata">

**Author:** ![liststuff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7bcc69/32.png) [@liststuff](https://www.ruby-forum.com/u/liststuff)\
**Post date:** [May 22, 2006, 1:47pm UTC](https://www.ruby-forum.com/t/text-field-with-auto-complete-and-record-id/49500/3 "2006-05-22T13:47:17Z")

</div>

Has anyone ever rewritten this, using “text\_field\_with\_auto\_complete”?

I am struggling with it, but can’t get it to work. I can retrieve a  
String, but not the ID. Help, anyone?

---

<div class="post-metadata">

**Author:** ![liststuff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7bcc69/32.png) [@liststuff](https://www.ruby-forum.com/u/liststuff)\
**Post date:** [May 22, 2006, 2:25pm UTC](https://www.ruby-forum.com/t/text-field-with-auto-complete-and-record-id/49500/4 "2006-05-22T14:25:23Z")

</div>

Hi !

2006/5/22, oom tuinstoel [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Has anyone ever rewritten this, using “text\_field\_with\_auto\_complete”?
> 
> I am struggling with it, but can’t get it to work. I can retrieve a  
> String, but not the ID. Help, anyone?

If I remember correctly, auto\_complete\_field now has support for  
afterUpdateElement. Look in the code:  
[http://dev.rubyonrails.org/browser/trunk/actionpack/lib/action\_view/helpers/java\_script\_macros\_helper.rb#L146](http://dev.rubyonrails.org/browser/trunk/actionpack/lib/action_view/helpers/java_script_macros_helper.rb#L146)

Bye !
