我想开发一个自动完成输入。
我有我的控制器动作:
def autocomplete_airports
render :json => WebService.get_airports(params[:airports_input])
end
get_airports返回以逗号(,)分隔的条目:
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
在我看来:
<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>
我的JavaScript:
$('#airports_input').autocomplete('/WebServices/autocomplete_airports');
但是,它不起作用。
我该怎么办?
谢谢!
答案 0 :(得分:0)
我认为您需要返回一组键值对才能使其正常工作。请查看@ my answer以查看最近的另一个问题。
我将添加此答案中未包含的相关型号代码(注意:Rails 3.0):
def self.search(search = nil)
if search
where('name LIKE ?', "%#{search}%") | tagged_with("%#{search}%")
else
scoped
end
end
def self.autocomplete(search)
results = search(search)
results.map! { |result| {:value => result.name} }
end
希望这有帮助。