简体   繁体   English

Rails + Ruby 1.9“ US-ASCII中无效的字节序”

[英]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: 似乎第一次起作用的修复程序:

  1. 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

  2. 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

  3. 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.

相关问题 US-ASCII中的无效字节序列(Ruby 1.9 + rails 2.3.8 + mongodb + mongo_mapper) - invalid byte sequence in US-ASCII (Ruby 1.9 + rails 2.3.8 + mongodb + mongo_mapper) 使用Rails和Ruby 1.9的无效多字节字符(US-ASCII) - invalid multibyte char (US-ASCII) with Rails and Ruby 1.9 将Ruby升级到1.9.2后,Rails查看错误“US-ASCII中的无效字节序列”错误 - Rails view error “invalid byte sequence in US-ASCII” error after upgrading Ruby to 1.9.2 RVM,Ruby 1.9.2,Rails 2.3.8,Passenger和“US-ASCII中的无效字节序列” - RVM, Ruby 1.9.2, Rails 2.3.8, Passenger and “invalid byte sequence in US-ASCII” 在US-ASCII问题中使用ruby 1.9.3的Rails 3.2无效字节序列 - Rails 3.2 with ruby 1.9.3 invalid byte sequence in US-ASCII issue ArgumentError(US-ASCII中无效的字节序列):在ruby升级1.9.3中 - ArgumentError (invalid byte sequence in US-ASCII): in ruby upgrade 1.9.3 Rails 3 无效的多字节字符(US-ASCII) - Rails 3 invalid multibyte char (US-ASCII) Rails:运行 rake db:seed 时 US-ASCII 中的字节序列无效(参数错误) - Rails: Invalid byte sequence in US-ASCII (Argument Error) when I run rake db:seed US-ASCII whith数组中的无效字节序列 - invalid byte sequence in US-ASCII whith array “无效的多字节字符(US-ASCII)”使用Ruby on Rails使用正则表达式验证新用户 - “invalid multibyte char (US-ASCII)” validating a new user with regex using Ruby on Rails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM