ActionView::Template::Error(不兼容的字符编码:UTF-8 和 ASCII-8BIT)

问题描述:

我使用的是 Ruby 1.9.2、Rails 3.0.4/3.0.5 和 Phusion 乘客 3.0.3/3.0.4.我的模板是用 HAML 编写的,我使用的是 MySQL2 gem.我有一个控制器动作,当传递一个具有特殊字符的参数(如变音符号)时,会出现以下错误:

I am using Ruby 1.9.2, Rails 3.0.4/3.0.5 and Phusion Passenger 3.0.3/3.0.4. My templates are written in HAML and I am using the MySQL2 gem. I have a controller action that when passed a parameter that has a special character, like an umlaut, gives me the following error:

ActionView::Template::Error (incompatible character encodings: UTF-8 and ASCII-8BIT)

错误指向我的 HAML 模板的第一行,上面有以下代码:

The error points to the first line of my HAML template, which has the following code on it:

<!DOCTYPE html>

我的理解是这是因为我有一个 UTF-8 字符串与一个 ASCII-8BIT 字符串连接,但我终生无法弄清楚那个 ASCII-8BIT 字符串是什么.我已经检查过操作中的参数是否使用 UTF-8 编码,并且我在 HAML 模板和 ruby​​ 文件的顶部添加了一个 encoding: UTF-8 声明,但我仍然收到此错误.我的 application.rb 文件中也有一个 config.encoding = "UTF-8" 声明,以下所有结果都为 UTF-8:

My understanding is that this is caused because I have a UTF-8 string that is being concatenated with an ASCII-8BIT string, but I can't for the life of me figure out what that ASCII-8BIT string is. I have checked that the params in the action are encoded using UTF-8 and I have added an encoding: UTF-8 declaration to the top of the HAML template and the ruby files and I still get this error. My application.rb file has a config.encoding = "UTF-8" declaration in it as well and the following all result in UTF-8:

ENV['LANG']
__ENCODING__
Encoding.default_internal
Encoding.default_external

问题在于:我无法在我的 Mac-OSX 上使用独立的乘客或杂种在开发或生产中本地重现此结果.我只能在 linux 上运行 nginx+passenger 的生产服务器上重现它.我已经在生产服务器的控制台中验证,后面提到的命令也会产生 UTF-8.

Here's the kicker: I cannot reproduce this result locally on my Mac-OSX using standalone passenger or mongrel in either development or production. I can only reproduce it on a production server running nginx+passenger on linux. I have verified in the production server's console that the latter mentioned commands all result in UTF-8 as well.

您是否遇到过同样的错误,您是如何解决的?

Have you experienced this same error and how did you solve it?

在进行一些调试后,我发现在使用 ActionDispatch::Request 对象时会出现问题,该对象恰好具有全部以 ASCII-8BIT 编码的字符串,无论我的应用程序是否以 UTF-8 编码.我不知道为什么只有在 Linux 上使用生产服务器时才会发生这种情况,但我假设这是 Ruby 或 Rails 中的一些怪癖,因为我无法在本地重现此错误.错误发生的原因是这样的一行:

After doing some debugging I found out the issue occurs when using the ActionDispatch::Request object which happens to have strings that are all coded in ASCII-8BIT, regardless of whether my app is coded in UTF-8 or not. I do not know why this only happens when using a production server on Linux, but I'm going to assume it's some quirk in Ruby or Rails since I was unable to reproduce this error locally. The error occurred specifically because of a line like this:

@current_path = request.env['PATH_INFO']

当在 HAML 模板中打印此实例变量时,它会导致错误,因为该字符串是以 ASCII-8BIT 而不是 UTF-8 编码的.为了解决这个问题,我做了以下事情:

When this instance variable was printed in the HAML template it caused an error because the string was encoded in ASCII-8BIT instead of UTF-8. To solve this I did the following:

@current_path = request.env['PATH_INFO'].dup.force_encoding(Encoding::UTF_8)

这迫使 @current_path 使用重复的字符串,该字符串被强制转换为正确的 UTF-8 编码.其他请求相关数据(如 request.headers)也可能发生此错误.

Which forced @current_path to use a duplicated string that was forced into the proper UTF-8 encoding. This error can also occur with other request related data like request.headers.