Replacing an HTML select tag with text_field_auto_complete + hidden field?

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"}) %>
  <label for="player_name">Player</label>
  <%= 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 Ludlow wrote:

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"}) %>
  <label for="player_name">Player</label>
  <%= 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

I usually do a lookup on the name in the controller

@player = Player.find_by_name(params[:player][:name])

_Kevin

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

-- James

James Ludlow wrote:

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 Ludlow wrote:

>
> James Ludlow 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?

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