I want to develop an autocomplete input.
I have my controller action for this:
def autocomplete_airports
render :json => WebService.get_airports(params[:airports_input])
end
get_airports returns entries separated by commas (,):
def self.get_airports(code)
#SOAP Action
begin response = @@client_airports_codes.request :tem, 'airports' do
soap.body =
{
"tem:prefixText" => code,
"tem:count" => @@airport_response_count
}
end
rescue Savon::SOAP::Fault => fault
puts fault.to_s
end
#preparing response
json = ""
response.to_hash[:airports_response][:airports_result][:string].each{
|key| json = json + key.to_s + ","
}
return json
end
In my view:
<form id="airport_form" class="center" action="">
<label for="airports_input">Airport/City</label>
<br />
<input type="text" name="airports_input" id="airports_input" />
</form>
And my JavaScript:
$('#airports_input').autocomplete('/WebServices/autocomplete_airports');
But, it doesn’t work.
what should I do?
Thank you!
I think you need to return an array of key-value pairs to make this work. Look @ my answer to to another recent question.
I’ll add the relevant model code not contained in that answer here (note: Rails 3.0):
Hope this helps.