[英]Rails + Ruby 1.9 “invalid byte squence in US-ASCII”
After upgrading to ruby 1.9 we began to notice pages failing to render from the rails template renderer when a user used a non-ASCII character. 升级到ruby 1.9之后,我们开始注意到当用户使用非ASCII字符时页面无法从rails模板渲染器渲染。 Specifically "é".
特别是“é”。 I was able to resolve this issue on one of our staging servers, but I have not been able to reproduce the fix on our production server.
我能够在一台登台服务器上解决此问题,但无法在生产服务器上重现此修复程序。
The fix that seemed to work the first time: 似乎第一次起作用的修复程序:
Converted the database from latin1 to utf8 using the convert_charset tool available here: http://www.mysqlperformanceblog.com/2009/03/17/converting-character-sets/ . 使用此处提供的convert_charset工具将数据库从latin1转换为utf8: http ://www.mysqlperformanceblog.com/2009/03/17/converting-character-sets/。 (including setting
default_character_set=utf8
in my.cnf and running SET GLOBAL character_set_server=utf8
(包括在my.cnf中设置
default_character_set=utf8
并运行SET GLOBAL character_set_server=utf8
Switched to the sam-mysql-ruby adapter (instead of the standard mysql adapter: http://gemcutter.org/gems/sam-mysql-ruby ) 切换到sam-mysql-ruby适配器(而不是标准mysql适配器: http : //gemcutter.org/gems/sam-mysql-ruby )
Restarted rails 重新启动滑轨
The error is: "invalid byte sequence in US-ASCII"
Oddly, after following the steps above the error has not changed on our production server. 错误是:
"invalid byte sequence in US-ASCII"
奇怪的是,按照上述步骤操作后,我们的生产服务器上的错误未更改。 Setting encoding: utf8
in database.yml does not change the error either. 在database.yml中设置
encoding: utf8
也不更改错误。
The error raised on the following line of code: <%= link_to h(question.title), question_path(question) %>
在以下代码行上引发的错误:
<%= link_to h(question.title), question_path(question) %>
This blog seems to suggest a fix, but it mentions that this should not be a problem in 1.9: http://www.igvita.com/2007/04/11/secure-utf-8-input-in-rails/ (and it's over 2 years old). 该博客似乎建议修复,但是它提到在1.9中应该不成问题: http : //www.igvita.com/2007/04/11/secure-utf-8-input-in-rails/ (并且已经超过2年)。
I imagine this problem might soon affect a lot of people as more rails developers people switch to 1.9. 我想随着更多的Rails开发人员切换到1.9,这个问题可能很快会影响很多人。
I found the solution: 我找到了解决方案:
The problem is: 问题是:
Fetching data from any database (Mysql, Postgresql, Sqlite2 & 3), all configured to have UTF-8 as it's character set, returns the data with ASCII-8BIT in ruby 1.9.1 and rails 2.3.2.1.
(Taken from: https://rails.lighthouseapp.com/projects/8994/tickets/2476 ) (摘自: https : //rails.lighthouseapp.com/projects/8994/tickets/2476 )
My attempt to use the patched mysql adapter likely failed because my database was not configured to natively use utf8, so the patched adapter failed to work properly. 我尝试使用修补的mysql适配器的尝试可能失败了,因为我的数据库未配置为本机使用utf8,因此修补的适配器无法正常工作。
The fix ended up being to use the patch file available here: http://gnuu.org/2009/11/06/ruby19-rails-mysql-utf8/ 该修复程序最终使用了此处提供的补丁文件: http : //gnuu.org/2009/11/06/ruby19-rails-mysql-utf8/
require 'mysql'
class Mysql::Result
def encode(value, encoding = "utf-8")
String === value ? value.force_encoding(encoding) : value
end
def each_utf8(&block)
each_orig do |row|
yield row.map {|col| encode(col) }
end
end
alias each_orig each
alias each each_utf8
def each_hash_utf8(&block)
each_hash_orig do |row|
row.each {|k, v| row[k] = encode(v) }
yield(row)
end
end
alias each_hash_orig each_hash
alias each_hash each_hash_utf8
end
(Placed in lib/mysql_utf8fix.rb and required in enviornment.rb using require 'lib/mysql_utf8fix.rb'
) (放置在lib / mysql_utf8fix.rb中,并在enviornment.rb中使用
require 'lib/mysql_utf8fix.rb'
)
它只需要'mysql_utf8fix.rb'(rails 2.3.11)
Please user mysql2(gem) adapter instead of mysql adapter in database.yml 请使用mysql2(gem)适配器而不是database.yml中的mysql适配器
and remove the mysql patches(If exists) and add the following lines in environment.rb. 并删除mysql补丁(如果存在),并在environment.rb中添加以下行。
Encoding.default_external = Encoding::UTF_8 Encoding.default_external =编码:: UTF_8
Encoding.default_internal = Encoding::UTF_8 Encoding.default_internal =编码:: UTF_8
Then run in apache and passenger it ll work fine 然后以阿帕奇(Apache)和乘客运行,它将正常工作
Thanks, 谢谢,
Ramanavel Selvaraju. 拉曼纳威尔(Ramanavel Selvaraju)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.