“警告无法确定响应正文的内容长度。”是什么意思?意思是我该如何摆脱它?
自从升级到 Rails 3.1 以来,我在开发日志中看到以下警告消息:
警告无法确定响应正文的内容长度。设置响应的内容长度或设置
Response#chunked = true
这是什么意思以及如何删除它?有问题吗?
Since upgrading to Rails 3.1 I'm seeing this warning message in my development log:
WARN Could not determine content-length of response body. Set content-length of the response or set
Response#chunked = true
What does this mean and how can I remove it? Is it a problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
向 Rails-Core 的一位成员提出了同样的问题:
https://twitter.com/luislavena/status/ 108998968859566080
答案:
https://twitter.com/tenderlove/status/108999110136303617
Asked the same question to one of Rails-Core's members:
https://twitter.com/luislavena/status/108998968859566080
And the answer:
https://twitter.com/tenderlove/status/108999110136303617
以下补丁解决了我的问题;不再对我发出警告。
204_304_keep_alive.patch
只需编辑第 205 行的文件 httpresponse.rb 即可,如图所示在上面的链接;事实上,该链接显示了对 Ruby 未来版本所做的更正。
我在通过 RVM 作为单个用户安装的 ruby 1.9.3-p0 上使用 Rails 3.2.0。所以我的例子中的位置是:
要更改的文件的位置根据安装类型、RVM 与否、甚至是多用户或单用户而有所不同,所以我只给出最后一部分:
我希望这对某人有帮助。
编辑:这是链接到更改主干分支中相关行的提交红宝石项目。
The following patch solved the problem in my case; no more warnings for me.
204_304_keep_alive.patch
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.
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:
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:
I hope this can be helpful to someone.
EDIT: This is the link to the commit that altered the line in question in the trunk branch of ruby project.
只需将 Gem 显式添加到 Gemfile 中即可消除警告消息:
Just explicitly adding the Gem to the Gemfile got rid of the warning messages for me:
您还可以使用 Thin 代替默认的 Webrick。
将其添加到
Gemfile
<代码>
宝石“薄”
那么
rails s Thin
将使用 Thin,并且警告将会消失。You can also use Thin instead of the default Webrick.
Add this to
Gemfile
gem 'thin'
then
rails s thin
will use thin, and the warning will disappear.如果您正在使用 .rvm,请执行此操作来修复它...
正如 João Soares 所提到的,所有功劳都归功于他,如果您愿意,这就是您可以做的不要消除这个关于发展的警告。
使用您最喜欢的编辑器打开此文件:
转到包含此文件的行(对我来说实际上是第 206 行):
更改它,取自 此补丁,对此:
保存文件并最终重新启动您的 Rails 服务器
If you're using .rvm, do this to fix it...
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.
Use your favorite editor to open this file:
Go to the line that contains this(for me it was really line 206):
Change it, taken from this patch, to this:
Save the file and eventually restart your rails server
此问题已在 Ruby 的主干分支中通过提交到 webrick 得到修复。
您可以在您的设置中类似地编辑这个特定的 webrick 文件。可以通过以下方式找到大致位置:
要实际编辑文件:(
或者使用您最喜欢的编辑器代替 nano。)
This problem has been fixed in Ruby's trunk branch with this commit to webrick.
You can edit this particular webrick file similarly in your setup. The approximate location can be found by:
To actually edit the file:
(Or instead of nano, use your favorite editor.)
JRuby 版本:如果您使用 .rvm,请执行此操作来修复它...
正如 João Soares 和 Kjellski 所提到的,这是如果您想在开发中消除此警告并且正在使用 JRuby,您可以做什么。
使用您最喜欢的编辑器打开此文件:
转到包含此文件的行(对我来说是第 205 行):
更改它,取自 此补丁,为此:
JRuby version: If you're using .rvm, do this to fix it...
As mentioned by João Soares and Kjellski, this is what you can do if you want to get rid of this warning on development and you are using JRuby.
Use your favorite editor to open this file:
Go to the line that contains this (for me it was line 205):
Change it, taken from this patch, to this:
Save the file and eventually restart your rails server.
另一种解决方法是从 webrick 中删除有问题的行。它只是没那么有用:(
您可能需要
sudo
)Another workaround that removes the offending line from webrick. It's just not that useful:
(you may need to
sudo
)添加
到您的
application.rb
文件中,即使使用 webrick,警告也会消失。当渲染 json 或文本响应时,这也将在生产中正确设置Content-Length
。Add
to your
application.rb
file, and the warning will disappear even with webrick. This will also setContent-Length
properly in production when rendering a json or text response.