Ruby on Rails和来自PSP的奇怪的HTTP_ACCEPT标头

我有Ruby on Rails应用程序(3.1rc4),我每天都会使用相同的用户代理(Mozilla / 4.0(PSP(PlayStation Portable); 2.00))获得一些例外。 例外:

A ActionView::MissingTemplate occurred in home#index: Missing template home/index, application/index with {:formats=>["*/*;q=0.01"], :locale=>[:en, :en], :handlers=>[:erb, :builder, :arb]}. Searched in: "/var/www/releases/20110721144523/app/views" 

我有app / views / home / index.html.erb,但看起来它试图找到一个非常奇怪的请求格式“ / ; q = 0.01”的文件。

HTTP标头:

  * HTTP_ACCEPT : */*;q=0.01 

任何人都可以帮我解决这个问题?

这是一个常见问题,请参阅Github上的票证 。

您可以选择显式呈现HTML; 只写render "index.html"而不是render 。 这将返回HTML而不是406。 我希望有更好的解决方案。

这是一个已知问题: https : //github.com/rails/rails/pull/4176您可以使用控制器中的respond_to和respond_with方法修复它。

我在使用中间件修复的Rails 3.0.x中修补了这个问题:

 class FixHttpAcceptHeader def initialize(app) @app = app end def call(env) case env['HTTP_ACCEPT'] when 'text/*', '*/*' # add others as needed env['HTTP_ACCEPT'] = 'text/html' end @app.call(env) end end 

在application.rb中

 config.middleware.use "FixHttpAcceptHeader" 

用这个ruby脚本测试(在网上找到这个脚本,但不记得在哪里…)

 require 'rubygems' require 'uri' require 'net/http' uri = URI.parse("http://localhost:3000") http = Net::HTTP.new(uri.host, uri.port) request = Net::HTTP::Get.new(uri.request_uri) request["Accept"] = "text/*" response = http.request(request) puts response.body