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