Replacing an HTML select tag with text_field_auto_complete +

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

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

On 9/2/06, _Kevin [email protected] 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

James L. wrote:

On 9/2/06, _Kevin [email protected] 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

On 9/2/06, Justin F. [email protected] 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 L. wrote:

On 9/2/06, Justin F. [email protected] 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

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

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