# Replacing an HTML select tag with text\_field\_auto\_complete +

**URL:** <https://www.ruby-forum.com/t/replacing-an-html-select-tag-with-text-field-auto-complete/67324>\
**Category:** Rails\
**Created:** [September 2, 2006, 7:29am UTC](https://www.ruby-forum.com/t/replacing-an-html-select-tag-with-text-field-auto-complete/67324 "2006-09-02T07:29:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jameslabocki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ad7895/32.png) [@jameslabocki](https://www.ruby-forum.com/u/jameslabocki)\
**Post date:** [September 2, 2006, 7:29am UTC](https://www.ruby-forum.com/t/replacing-an-html-select-tag-with-text-field-auto-complete/67324/1 "2006-09-02T07:29:57Z")

</div>

I’m trying to replace an HTML select tag with an auto-completing text  
field. This field is part of a larger form. The original select is  
used to choose a player. This works, but as the number of players  
grows the number of option tags becomes way too big. So, it seemed  
like a great idea to provide an auto-completing type ahead.

The problem that I’m having is that when I submit my form I don’t  
really care about the player name. I want the player id (much like  
when using a select). What I’m trying to do is populate a hidden  
field with the player id when the user chooses a player name.

My latest attempt involves playing with :after\_update\_element. I  
can’t really find anything resembling good documentation for this, so  
I’m kind-of guessing. Is there anything that I can add in the  
JavaScript function that will populate the hidden id field?

\<%= start\_form\_tag ({:action =\> ‘log’, :id =\> @player}, {:AUTOCOMPLETE  
=\> “off”}) %\>  
Player  
\<%= text\_field\_with\_auto\_complete :player,  
:name,  
{},  
{:after\_update\_element =\> “function(element,value){ …???.. }”}  
%\>

\<%= hidden\_field :player, :id %\>  
\<%= end\_form\_tag %\>

def auto\_complete\_for\_player\_name  
name = params[:player][:name]  
query = ‘%’ + name + ‘%’  
@players = Player.find(:all,  
:conditions =\> [‘firstname ilike ? or  
lastname ilike ? or nickname ilike ?’, query, query, query],  
:limit =\> 15,  
:order =\> “lastname”)

```
render :inline => "<%= auto_complete_result(@players, 'firstname') 

```

%\>"  
end

The dropdown works fine. I see the search results that I expect, the  
arrow keys work, selection with Enter works, etc. I just can’t figure  
out how to populate the id.

Thanks,  
– James

---

<div class="post-metadata">

**Author:** ![jameslabocki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ad7895/32.png) [@jameslabocki](https://www.ruby-forum.com/u/jameslabocki)\
**Post date:** [September 2, 2006, 1:24pm UTC](https://www.ruby-forum.com/t/replacing-an-html-select-tag-with-text-field-auto-complete/67324/2 "2006-09-02T13:24:17Z")

</div>

James L. wrote:

> ```
> :name,
> @players = Player.find(:all,
> 
> ```
> 
> arrow keys work, selection with Enter works, etc. I just can’t figure  
> out how to populate the id.
> 
> Thanks,  
> – James

I usually do a lookup on the name in the controller

@player = Player.find\_by\_name(params[:player][:name])

\_Kevin

---

<div class="post-metadata">

**Author:** ![jameslabocki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ad7895/32.png) [@jameslabocki](https://www.ruby-forum.com/u/jameslabocki)\
**Post date:** [September 2, 2006, 3:30pm UTC](https://www.ruby-forum.com/t/replacing-an-html-select-tag-with-text-field-auto-complete/67324/3 "2006-09-02T15:30:26Z")

</div>

On 9/2/06, \_Kevin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I usually do a lookup on the name in the controller
> 
> @player = Player.find\_by\_name(params[:player][:name])

That would assume that names are unique, which is not true. That’s  
why I need to pass in the ID.

– James

---

<div class="post-metadata">

**Author:** ![jameslabocki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ad7895/32.png) [@jameslabocki](https://www.ruby-forum.com/u/jameslabocki)\
**Post date:** [September 2, 2006, 5:08pm UTC](https://www.ruby-forum.com/t/replacing-an-html-select-tag-with-text-field-auto-complete/67324/4 "2006-09-02T17:08:33Z")

</div>

James L. wrote:

> On 9/2/06, \_Kevin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I usually do a lookup on the name in the controller
> > 
> > @player = Player.find\_by\_name(params[:player][:name])
> 
> That would assume that names are unique, which is not true. That’s  
> why I need to pass in the ID.

If the names in the autocomplete field aren’t unique, how does the user  
know he or she is making the right selection?

regards

Justin

---

<div class="post-metadata">

**Author:** ![jameslabocki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ad7895/32.png) [@jameslabocki](https://www.ruby-forum.com/u/jameslabocki)\
**Post date:** [September 3, 2006, 2:45am UTC](https://www.ruby-forum.com/t/replacing-an-html-select-tag-with-text-field-auto-complete/67324/5 "2006-09-03T02:45:33Z")

</div>

On 9/2/06, Justin F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If the names in the autocomplete field aren’t unique, how does the user  
> know he or she is making the right selection?

Because I’m showing more than just the name in the auto complete div.  
I thought about just adding the ID to the name and then parsing it out  
when the form is submitted. I still may end up with this, but it  
seems ugly.

– James

---

<div class="post-metadata">

**Author:** ![jameslabocki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ad7895/32.png) [@jameslabocki](https://www.ruby-forum.com/u/jameslabocki)\
**Post date:** [September 3, 2006, 9:02pm UTC](https://www.ruby-forum.com/t/replacing-an-html-select-tag-with-text-field-auto-complete/67324/6 "2006-09-03T21:02:48Z")

</div>

James L. wrote:

> On 9/2/06, Justin F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > If the names in the autocomplete field aren’t unique, how does the user  
> > know he or she is making the right selection?
> 
> Because I’m showing more than just the name in the auto complete div.  
> I thought about just adding the ID to the name and then parsing it out  
> when the form is submitted. I still may end up with this, but it  
> seems ugly.
> 
> – James

James,

Do you still need help with this? I was able to do it using something  
sort of similar to what you had posted above.

I think our needs are very much alike. For purposes of example, I have  
an order and I need to associate a contact to it. I want to associate  
the id of the contact to the order.

My partial rhtml looks like this (very simple for demo):

\<% for contact in @contacts do -%\> - 
\<%=h contact.full\_name %\>

\<%=h contact.phone\_number %\>
\<% end -%\>

Then, my view looks like this:

```
                    <%= hidden_field :order, :contact_id %>
                    <%= text_field_with_auto_complete :order, 

```

:contact\_name,  
{}, :skip\_style =\> true %\>

When I save, the contact id is saved on the order.

Is this helpful?

Michael

---

<div class="post-metadata">

**Author:** ![jameslabocki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ad7895/32.png) [@jameslabocki](https://www.ruby-forum.com/u/jameslabocki)\
**Post date:** [September 4, 2006, 2:43pm UTC](https://www.ruby-forum.com/t/replacing-an-html-select-tag-with-text-field-auto-complete/67324/7 "2006-09-04T14:43:36Z")

</div>

James L. wrote:

> > > why I need to pass in the ID.
> > 
> > If the names in the autocomplete field aren’t unique, how does the user  
> > know he or she is making the right selection?
> 
> Because I’m showing more than just the name in the auto complete div.  
> I thought about just adding the ID to the name and then parsing it out  
> when the form is submitted. I still may end up with this, but it  
> seems ugly.
> 
> – James

If you have enough info in the auto\_complete for the user to know the  
record is unique, then you should be able to find the record through  
the text as well. If the submitted param looks something like ‘player  
name - Team’, then just split the param before doing the look-up.

@player = Player.find\_by\_name\_and\_team(name, team)

It would be nice if the auto\_complete sent back the id of the match,  
but I don’t think it does that right now.

\_Kevin

---

<div class="post-metadata">

**Author:** ![jameslabocki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ad7895/32.png) [@jameslabocki](https://www.ruby-forum.com/u/jameslabocki)\
**Post date:** [September 4, 2006, 6:33pm UTC](https://www.ruby-forum.com/t/replacing-an-html-select-tag-with-text-field-auto-complete/67324/8 "2006-09-04T18:33:38Z")

</div>

> It would be nice if the auto\_complete sent back the id of the match,  
> but I don’t think it does that right now.
> 
> \_Kevin

Kevin,

Read my reply to this question as I was able to get the id by using a  
hidden field!

Michael
