我是 Rails 新手,我整天都在寻找答案。让我们从乞讨开始。
UI:我有自动完成功能(准确地说,是页面上的 YUI 自动完成功能)。
Y.one('input.search-from').plug(Y.Plugin.AutoComplete, {
resultHighlighter: 'phraseMatch',
source: "/api/open_maps/search/{query}",
resultTextLocator: 'display_name'
}
其中“/api/open_maps/search/{query}”转到rails,在我的情况下,它被用作解决ajax同源策略的代理。
def search
uri = URI.parse(searchUrl(CGI.escape(params[:query])))
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
render :json => response.body
end
如果我快速键入,则从自动完成器到 rails 的所有 ajax 请求都将被取消,这是有道理的,因为我们不再需要它们。问题是 Rails 为所有取消的请求抛出了丑陋的异常:
Started GET "/api/open_maps/search/Den" for 127.0.0.1 at Sun May 06 04:20:03 +0100 2012
[2012-05-06 04:20:03] ERROR Errno::EINVAL: Invalid argument
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/httpresponse.rb:324:in `write'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/httpresponse.rb:324:in `<<'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/httpresponse.rb:324:in `_write_data'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/httpresponse.rb:296:in `send_body_string'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/httpresponse.rb:187:in `send_body'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/httpresponse.rb:104:in `send_response'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/httpserver.rb:79:in `run'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:162:in `start'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:95:in `start'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:92:in `each'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:92:in `start'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:23:in `start'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:82:in `start'
/Library/Ruby/Gems/1.8/gems/rack-1.4.1/lib/rack/handler/webrick.rb:13:in `run'
/Library/Ruby/Gems/1.8/gems/rack-1.4.1/lib/rack/server.rb:265:in `start'
/Library/Ruby/Gems/1.8/gems/railties-3.2.3/lib/rails/commands/server.rb:70:in `start'
/Library/Ruby/Gems/1.8/gems/railties-3.2.3/lib/rails/commands.rb:55
/Library/Ruby/Gems/1.8/gems/railties-3.2.3/lib/rails/commands.rb:50:in `tap'
/Library/Ruby/Gems/1.8/gems/railties-3.2.3/lib/rails/commands.rb:50
script/rails:6:in `require'
script/rails:6
此外,看起来红宝石无论如何都会在后台发出取消请求(这会减慢一切)。
我的问题是:
- 我们如何在 Rails 中检测到来自客户端的连接失败/被取消?
- 一旦我们了解了#1,我们如何取消 Net::HTTP 发出的请求?
我尝试了开始->救援->结束块以至少消除异常,但没有帮助。