Thursday, December 1, 2011

Consuming ID returned by JQuery Autocomplete

This autocompete definition in Participants controller:

autocomplete :participant, :full_name, :display_value => :full_name, :full => true

and this pair of Rails tags:

<%= f.autocomplete_field :name,  
     autocomplete_participant_name_students_path, 
     :id_element => '#participantt_id' %>
  
<%= hidden_field :participant, :id %>

resulting in this HTML

<input data-autocomplete="/participant/autocomplete_participant_full_name"      
  id="full_name" 
  id_element="#participant_id"
  name="full_name" type="text" value="" />  
<input id="participant_id" name="participant[id]" type="hidden" />

does the trick for delivering ID of object found from autocomplete into hidden input field. The value is, then, of course, available as a request parameter when enclosing form is submitted. Your controller accesses this param as such:

Participant.find(params[:participant][:id])


I have tested this in both 0.6.1 and 1.0.4 versions of rails3-jquery-autocomplete gem, using Rails 3.1.1.

No comments:

Post a Comment