Rails + Ruby 1.9“US-ASCII中的无效字节间隔” [英] Rails + Ruby 1.9 "invalid byte squence in US-ASCII"

查看:115
本文介绍了Rails + Ruby 1.9“US-ASCII中的无效字节间隔”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

升级到ruby 1.9后,当用户使用非ASCII字符时,我们开始注意到无法从rails模板渲染器渲染的页面。特别是é。我可以在我们的某个临时服务器上解决此问题,但是我无法在我们的生产服务器上重现此修复程序。



似乎有效的修复程序第一次:


  1. 使用convert_charset工具将数据库从latin1转换为utf8: http://www.mysqlperformanceblog.com/2009/03/17/converting-character-sets/ 。 (包括在my.cnf中设置 default_character_set = utf8 并运行 SET GLOBAL character_set_server = utf8


  2. 切换到sam-mysql-ruby适配器(而不是标准的mysql适配器: http://gemcutter.org/gems/sam-mysql-ruby


  3. 重新启动的栏杆


错误是:
US-ASCII中的无效字节序列
奇怪的是,在上面的步骤之后,错误在我们的生产服务器上没有改变。在database.yml中设置 encoding:utf8



在下面一行代码中引发的错误:
<%= link_to h(question.title),question_path (问题)%>



此博客似乎建议修复,但它提到这不应该是一个问题在1.9: a href =http://www.igvita.com/2007/04/11/secure-utf-8-input-in-rails/ =nofollow noreferrer> http://www.igvita.com/ 2007/04/11 / secure-utf-8-input-in-rails / (超过2岁)。



解决方案

我发现了解决方案:



问题是:



从任何数据库获取数据(Mysql,Postgresql,Sqlite2& 3),所有配置为使用UTF-8作为字符集,在ruby 1.9.1和rails 2.3.2.1中返回ASCII-8BIT的数据。
(来自: https://rails.lighthouseapp.com/projects/8994/tickets/2476



我尝试使用修补的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.force_encoding(encoding):value
end

def each_utf8(& block)
each_orig do | row |
yield row.map {| col | encode(col)}
end
end
别名each_orig每个
别名每个each_utf8

def each_hash_utf8(& block)
each_hash_orig do | row |
row.each {| k,v | row [k] = encode(v)}
yield(row)
end
end
别名each_hash_orig each_hash
别名each_hash each_hash_utf8
end

(放在lib / mysql_utf8fix.rb中,enviornment.rb中需要使用 require'lib /mysql_utf8fix.rb'


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. 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/. (including setting default_character_set=utf8 in my.cnf and running 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)

  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. Setting encoding: utf8 in database.yml does not change the error either.

The error raised on the following line of code: <%= 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).

I imagine this problem might soon affect a lot of people as more rails developers people switch to 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)

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.

The fix ended up being to use the patch file available here: 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')

这篇关于Rails + Ruby 1.9“US-ASCII中的无效字节间隔”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆