JQuery 的 RoR 2.3.9 自动完成功能
我想开发一个自动完成输入。
我有这样的控制器操作:
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');
但是,它不起作用。
我应该怎么办?
谢谢你!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您需要返回一个键值对数组才能完成这项工作。看看@ 我的答案 到另一个最近的问题。
我将在此处添加该答案中未包含的相关模型代码(注意:Rails 3.0):
希望这会有所帮助。
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.