# Can text\_field\_with\_auto\_complete return ID?

**URL:** <https://www.ruby-forum.com/t/can-text-field-with-auto-complete-return-id/49089>\
**Category:** Rails\
**Created:** [November 29, 2005, 6:31pm UTC](https://www.ruby-forum.com/t/can-text-field-with-auto-complete-return-id/49089 "2005-11-29T18:31:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![carlos.okada](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/22d042/32.png) [@carlos.okada](https://www.ruby-forum.com/u/carlos.okada)\
**Post date:** [November 29, 2005, 6:31pm UTC](https://www.ruby-forum.com/t/can-text-field-with-auto-complete-return-id/49089/1 "2005-11-29T18:31:03Z")

</div>

Hello

Can I make text\_field\_with\_auto\_complete show the name atribute and then  
return the ID of the selected object when the page is posted-back?

Thanks!

Okada.

---

<div class="post-metadata">

**Author:** ![carlos.okada](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/22d042/32.png) [@carlos.okada](https://www.ruby-forum.com/u/carlos.okada)\
**Post date:** [June 6, 2006, 5:22pm UTC](https://www.ruby-forum.com/t/can-text-field-with-auto-complete-return-id/49089/2 "2006-06-06T17:22:51Z")

</div>

I am having the same issue…

Can anyone help?

---

<div class="post-metadata">

**Author:** ![carlos.okada](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/22d042/32.png) [@carlos.okada](https://www.ruby-forum.com/u/carlos.okada)\
**Post date:** [June 6, 2006, 10:35pm UTC](https://www.ruby-forum.com/t/can-text-field-with-auto-complete-return-id/49089/3 "2006-06-06T22:35:27Z")

</div>

carlos.okada wrote:

> Hello
> 
> Can I make text\_field\_with\_auto\_complete show the name atribute and then  
> return the ID of the selected object when the page is posted-back?
> 
> Thanks!
> 
> Okada.

A textfield just returns it’s contents. It’s just how HTML works. What  
you can do is match it up server side.

object = SomeModel.find(:first,  
:conditions =\> [‘title = ?’,  
params[:data\_from\_textbox]])

---

<div class="post-metadata">

**Author:** ![carlos.okada](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/22d042/32.png) [@carlos.okada](https://www.ruby-forum.com/u/carlos.okada)\
**Post date:** [June 6, 2006, 11:11pm UTC](https://www.ruby-forum.com/t/can-text-field-with-auto-complete-return-id/49089/4 "2006-06-06T23:11:39Z")

</div>

Shai S. wrote:

> I am having the same issue…
> 
> Can anyone help?

The trick that I use is to return the id as a hidden field. Here is an  
example of \_full\_names.rhtml I used as part of the Contacts demo I am  
working on.

\<% for contact in @contacts do -%\> - 
\<%= contact.id %\>

\<%=h "#{contact.first\_name} #{contact.last\_name}" %\>

 \<%=h "#{contact.address}" %\>  
 \<%=h "#{contact.city}, #{contact.state} #{contact.zip\_code}" %\> 
\<% end -%\>

I then take the resulting value returned when the user clicks the  
appropriate item and convert it to an integer which I can use to find  
the correct Contact entry. Using this method I can then update multiple  
fields.

---

<div class="post-metadata">

**Author:** ![carlos.okada](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/22d042/32.png) [@carlos.okada](https://www.ruby-forum.com/u/carlos.okada)\
**Post date:** [June 6, 2006, 11:14pm UTC](https://www.ruby-forum.com/t/can-text-field-with-auto-complete-return-id/49089/5 "2006-06-06T23:14:18Z")

</div>

Alex W. wrote:

> carlos.okada wrote:
> 
> > Hello
> > 
> > Can I make text\_field\_with\_auto\_complete show the name atribute and then  
> > return the ID of the selected object when the page is posted-back?
> > 
> > Thanks!
> > 
> > Okada.
> 
> A textfield just returns it’s contents. It’s just how HTML works. What  
> you can do is match it up server side.
> 
> object = SomeModel.find(:first,  
> :conditions =\> [‘title = ?’,  
> params[:data\_from\_textbox]])

This only works if the text contents returned are unique enough. If your  
data is more complex or if you have multiple similar entries, this  
doesn’t work well.

---

<div class="post-metadata">

**Author:** ![carlos.okada](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/22d042/32.png) [@carlos.okada](https://www.ruby-forum.com/u/carlos.okada)\
**Post date:** [June 7, 2006, 6:28pm UTC](https://www.ruby-forum.com/t/can-text-field-with-auto-complete-return-id/49089/6 "2006-06-07T18:28:20Z")

</div>

Dale M. wrote:

> The trick that I use is to return the id as a hidden field. Here is an  
> example of \_full\_names.rhtml I used as part of the Contacts demo I am  
> working on.
> 
> \<% for contact in @contacts do -%\> - 
> \<%= contact.id %\>
> 
> \<%=h "#{contact.first\_name} #{contact.last\_name}" %\>
> 
> \<%=h "#{contact.address}" %\>  
> \<%=h "#{contact.city}, #{contact.state} #{contact.zip\_code}" %\> 
> \<% end -%\>
> 
> I then take the resulting value returned when the user clicks the  
> appropriate item and convert it to an integer which I can use to find  
> the correct Contact entry. Using this method I can then update multiple  
> fields.

I understand the concept and I tried out some code, but it does not seem  
to work as in the example… Would you be able to post a little of your  
controller and view code so I could compare and possibly see what I did  
wrong?

To help better understand, Ill post mine as well…

I want to search for a product title and when I click “see” I want to go  
to that products page…

In my view I have:

\<%= start\_form\_tag :action =\> ‘view’, :id =\> @search\_item %\>

\<%= text\_field\_with\_auto\_complete :product, :title, :size =\> 40 ,  
:skip\_style =\> true %\>

\<%= end\_form\_tag %\>

And then, in the Controller:

def auto\_complete\_for\_product\_title  
auto\_complete\_responder\_for\_products params[:title]  
end

private

def auto\_complete\_responder\_for\_products(value)  
@search\_results = Product.find(:all,  
:conditions =\> [ ‘LOWER(title) LIKE ?’,  
‘%’ + value.downcase + ‘%’ ],  
:order =\> ‘title ASC’,  
:limit =\> 8)  
render :partial =\> ‘products’  
end

And in my partial:

\<% for search\_item in @search\_results do -%\> - 
\<%= search\_item.id %\>

 \<%= image\_tag url\_for\_file\_column "search\_item", "art", "50" -%\> 

\<%= search\_item.title %\>
\<% end -%\>

Could anyone offer some insight as to what may be wrong? I will say that  
when looking at the scriptaculous example I do get a bit confused with  
the :message params that are passed… what are they needed for?

Thanks

---

<div class="post-metadata">

**Author:** ![carlos.okada](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/22d042/32.png) [@carlos.okada](https://www.ruby-forum.com/u/carlos.okada)\
**Post date:** [December 31, 2009, 8:53pm UTC](https://www.ruby-forum.com/t/can-text-field-with-auto-complete-return-id/49089/7 "2009-12-31T20:53:35Z")

</div>

I forked DHH’s auto\_complete plugin to add the record id to the id tag  
of the

- element. There is an example in the README of how to use the  
id using :after\_update\_element. Check it out on GitHub at  
[GitHub - jmaziarz/auto\_complete: Auto Completion plugin](http://github.com/jmaziarz/auto_complete).

---

<div class="post-metadata">

**Author:** ![carlos.okada](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/22d042/32.png) [@carlos.okada](https://www.ruby-forum.com/u/carlos.okada)\
**Post date:** [June 8, 2006, 8:22am UTC](https://www.ruby-forum.com/t/can-text-field-with-auto-complete-return-id/49089/8 "2006-06-08T08:22:17Z")

</div>

Shai S. wrote:

> Dale M. wrote:
> 
> > The trick that I use is to return the id as a hidden field. Here is an  
> > example of \_full\_names.rhtml I used as part of the Contacts demo I am  
> > working on.
> > 
> > \<% for contact in @contacts do -%\> - 
> > \<%= contact.id %\>
> > 
> > \<%=h "#{contact.first\_name} #{contact.last\_name}" %\>
> > 
> > \<%=h "#{contact.address}" %\>  
> > \<%=h "#{contact.city}, #{contact.state} #{contact.zip\_code}" %\> 
> > \<% end -%\>
> > 
> > I then take the resulting value returned when the user clicks the  
> > appropriate item and convert it to an integer which I can use to find  
> > the correct Contact entry. Using this method I can then update multiple  
> > fields.
> 
> I understand the concept and I tried out some code, but it does not seem  
> to work as in the example… Would you be able to post a little of your  
> controller and view code so I could compare and possibly see what I did  
> wrong?

I posted a description of what I am doing on my blog. See if this helps,  
if not I will create a demo Rails app for you to look at.

[http://www.dalemartenson.com/blog/?p=24](http://www.dalemartenson.com/blog/?p=24)

Here is a the parts that I use to get the id of an object and update  
multiple fiields.

index.rthml-------------------

 \<%= javascript\_include\_tag :defaults %\> 

Look-up Contact By Full Name: \<%= render :partial =\> 'get\_full\_name' %\>

* * *

 \<%= render :partial =\> 'get\_name\_info' %\> \<%= render :partial =\> 'get\_address\_info' %\> \<%= render :partial =\> 'get\_phone\_info' %\> \<%= render :partial =\> 'get\_internet\_info' %\> 

EOF---------------------------

I like using render :partial for all the pieces of the displayed page.  
The autocomplete field is in the \_get\_full\_name.rthml partial.

\_get\_full\_name.rhtml----------

\<%= text\_field\_with\_auto\_complete :contact, :full\_name, {}, :after\_update\_element =\> "function(element,value) " + "{ " + remote\_function(:update=\>'full\_name',:url=\>{:action=\>:get\_full\_name},:with=\>"'id='+element.value") + ";" + remote\_function(:update=\>'name\_info',:url=\>{:action=\>:get\_name\_info},:with=\>"'id='+element.value") + ";" + remote\_function(:update=\>'address\_info',:url=\>{:action=\>:get\_address\_info},:with=\>"'id='+element.value") + ";" + remote\_function(:update=\>'phone\_info',:url=\>{:action=\>:get\_phone\_info},:with=\>"'id='+element.value") + ";" + remote\_function(:update=\>'internet\_info',:url=\>{:action=\>:get\_internet\_info},:with=\>"'id='+element.value") + ";" + "}" %\>

EOF---------------------------

This uses the after\_update\_element to force the updating of other  
corresponding pieces of the page being displayed. The id is past from  
the element.value which is really a string based on list item contents  
that were shown to the user. This generated Javascript function controls  
getting the data for the updates and helps to perform the multiple  
partial updates.

Note: I reference full\_name as if it were a field in my contacts table,  
but it isn’t. I have added a member function to the Contacts model to  
combine the first\_name and last\_name fields into a full\_name.

def full\_name  
“#{self.first\_name} #{self.last\_name}”  
end

Since I want to perform full name searches using a single field, I  
require a custom handler be added to my controller.

def auto\_complete\_for\_contact\_full\_name  
@contacts = Contact.find(:all,  
:conditions =\> [ ‘LOWER(CONCAT(first\_name," ",last\_name)) LIKE ?’,  
‘%’ + params[:contact][:full\_name].downcase + ‘%’ ],  
:order =\> ‘first\_name ASC’,  
:limit =\> 8)  
render :partial =\> ‘full\_names’  
end

The list item content is controlled by the \_full\_names.rhtml partial in  
this example.

\_full\_names.rhtml--------------

\<% for contact in @contacts do -%\> - 
\<%= contact.id %\>

\<%=h "#{contact.first\_name} #{contact.last\_name}" %\>

 \<%=h "#{contact.address}" %\>  
 \<%=h "#{contact.city}, #{contact.state} #{contact.zip\_code}" %\> 
\<% end -%\>

EOF---------------------------

Rather than just display a simple list of names. I display a bold full  
name and a complete address in a small font to help uniquely identify  
the individual. But the important part is what is not seen by the user.  
The first part of my list item is a non-displayed text string of the  
object id. This uniquely identifies the item without having to re-query  
the database which can be problematic depending on the data uniqueness  
of the data and if multiple entries may exist.

The string returned when the user selects an entry looks something like  
“\n 123\n Dale M.\n 1234 Blah Street\n Somewhere, Someplace  
12345”. As you will see, I can take thing string and convert it to an  
integer to ‘steal’ the first id portion while ignoring the rest. From  
the generated Javascript above, this is the result in element.value that  
I pass as a parameter back to my controller methods.

All the controller methods are very similar.

def get\_address\_info  
@contact = Contact.find(params[:id].to\_i)  
render :partial =\> ‘get\_address\_info’  
end

As you can see, it takes the id parameter converts it to an integer and  
finds the correct object based on the id. Then the corresponding partial  
is rendered to complete the updating of that portion of the display.

\_get\_address\_info.rhtml-------

 Address Information Address: \<%= text\_field :contact, :address %\>  
 City: \<%= text\_field :contact, :city %\> State: \<%= text\_field :contact, :state %\> Zip: \<%= text\_field :contact, :zip\_code %\> 

EOF---------------------------

There may be a more elegant way to do the update, but I have yet to  
determine one. If anyone has any suggestions for improving this please  
let me know.
