Ruby-on-rails “警告无法确定响应正文的内容长度”是什么意思。意思是我如何摆脱它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7082364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does "WARN Could not determine content-length of response body." mean and how to I get rid of it?
提问by Nate Bird
Since upgrading to Rails 3.1 I'm seeing this warning message in my development log:
升级到 Rails 3.1 后,我在开发日志中看到了这条警告消息:
WARN Could not determine content-length of response body. Set content-length of the response or set
Response#chunked = true
WARN 无法确定响应正文的内容长度。设置响应的内容长度或设置
Response#chunked = true
What does this mean and how can I remove it? Is it a problem?
这是什么意思,我该如何删除它?这是个问题吗?
采纳答案by Luis Lavena
Asked the same question to one of Rails-Core's members:
向 Rails-Core 的一位成员提出了同样的问题:
https://twitter.com/luislavena/status/108998968859566080
https://twitter.com/luislavena/status/108998968859566080
And the answer:
答案是:
https://twitter.com/tenderlove/status/108999110136303617
https://twitter.com/tenderlove/status/108999110136303617
ya, it's fine. Need to clean it up, but nothing is being hurt.
是的,没关系。需要清理它,但没有任何伤害。
回答by jasoares
The following patch solved the problemin my case; no more warnings for me.
以下补丁解决了我的问题;没有更多的警告给我。
Just edit the file httpresponse.rb at line 205 as shown at the link above; in fact the link shows a correction made to a future release of Ruby.
只需编辑文件 httpresponse.rb 的第 205 行,如上面的链接所示;事实上,该链接显示了对未来 Ruby 版本所做的更正。
I'm using rails 3.2.0 on ruby 1.9.3-p0 installed through RVM as a single user. So the location in my case is:
我在作为单个用户通过 RVM 安装的 ruby 1.9.3-p0 上使用 rails 3.2.0。所以在我的情况下的位置是:
~/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/httpresponse.rb
The location of the file to be altered differs depending on the type of installation, RVM or not, or even multi-user or single user, so I'm just giving the last part of it:
要更改的文件的位置因安装类型、RVM 与否,甚至是多用户或单用户而异,所以我只给出最后一部分:
.../ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/httpresponse.rb
I hope this can be helpful to someone.
我希望这可以对某人有所帮助。
EDIT: This is the linkto the commit that altered the line in question in the trunk branch of ruby project.
编辑:这是更改 ruby 项目主干分支中相关行的提交的链接。
回答by ootoovak
Just explicitly adding the Gem to the Gemfile got rid of the warning messages for me:
只需将 Gem 显式添加到 Gemfile 即可为我消除警告消息:
group :development do
gem 'webrick', '~> 1.3.1'
end
回答by Cam Song
You can also use Thin instead of the default Webrick.
Add this to Gemfilegem 'thin'
您还可以使用 Thin 而不是默认的 Webrick。将此添加到Gemfilegem 'thin'
then rails s thinwill use thin, and the warning will disappear.
然后rails s thin将使用薄,警告将消失。
回答by Kjellski
If you're using .rvm, do this to fix it...
如果您使用的是 .rvm,请执行此操作以修复它...
As mentioned by Jo?o Soares, all credits to him, this is what you can do if you wan't to get rid of this warning on development.
正如Jo?o Soares所提到的,所有功劳都归功于他,如果你不想摆脱这个关于开发的警告,这就是你可以做的。
Use your favorite editor to open this file:
~/.rvm/rubies/<ruby-version>/lib/ruby/1.9.1/webrick/httpresponse.rbGo to the line that contains this(for me it was really line 206):
if chunked? || @header['content-length']Change it, taken from this patch, to this:
if chunked? || @header['content-length'] || @status == 304 || @status == 204Save the file and eventually restart your rails server
使用您喜欢的编辑器打开此文件:
~/.rvm/rubies/<ruby-version>/lib/ruby/1.9.1/webrick/httpresponse.rb转到包含此内容的行(对我而言,它实际上是第 206 行):
if chunked? || @header['content-length']将它从这个 patch更改为:
if chunked? || @header['content-length'] || @status == 304 || @status == 204保存文件并最终重新启动 Rails 服务器
回答by MarkDBlackwell
This problem has been fixed in Ruby's trunk branch with this committo webrick.
这个问题已经在 Ruby 的主干分支中通过对 webrick 的提交得到修复。
You can edit this particular webrick file similarly in your setup. The approximate location can be found by:
您可以在设置中类似地编辑这个特定的 webrick 文件。大概位置可以通过以下方式找到:
gem which webrick
To actually edit the file:
要实际编辑文件:
nano \`ruby -e"print %x{gem which webrick}.chomp %Q{.rb\n}"\`/httpresponse.rb
(Or instead of nano, use your favorite editor.)
(或者代替 nano,使用您最喜欢的编辑器。)
回答by Crimbo
JRuby version: If you're using .rvm, do this to fix it...
JRuby 版本:如果您使用的是 .rvm,请执行此操作以修复它...
As mentioned by Jo?o Soaresand Kjellski, this is what you can do if you want to get rid of this warning on development and you are using JRuby.
正如Jo?o Soares和Kjellski所提到的,如果您想在开发中摆脱这个警告并且您正在使用 JRuby,那么您可以这样做。
Use your favorite editor to open this file:
~/.rvm/rubies/jruby-<version>/lib/ruby/<1.8 or 1.9>/webrick/httpresponse.rbGo to the line that contains this (for me it was line 205):
if chunked? || @header['content-length']Change it, taken from this patch, to this:
if chunked? || @header['content-length'] || @status == 304 || @status == 204Save the file and eventually restart your rails server.
使用您喜欢的编辑器打开此文件:
~/.rvm/rubies/jruby-<version>/lib/ruby/<1.8 or 1.9>/webrick/httpresponse.rb转到包含此内容的行(对我来说是第 205 行):
if chunked? || @header['content-length']将它从这个 patch更改为:
if chunked? || @header['content-length'] || @status == 304 || @status == 204保存文件并最终重新启动 Rails 服务器。
回答by Michael Franzl
Add
添加
config.middleware.use Rack::ContentLength
to your application.rbfile, and the warning will disappear even with webrick. This will also set Content-Lengthproperly in production when rendering a json or text response.
到您的application.rb文件,即使使用 webrick,警告也会消失。Content-Length在渲染 json 或文本响应时,这也将在生产中正确设置。
回答by Xavier Shay
Another workaround that removes the offending line from webrick. It's just not that useful:
从 webrick 中删除违规行的另一种解决方法。它只是没有那么有用:
cd `which ruby`/../../lib/ruby/1.9.1/webrick/ && sed -i '.bak' -e'/logger.warn/d' httpresponse.rb
(you may need to sudo)
(你可能需要sudo)

