ActionView::TemplateError (incompatible character encodings: ASCII-8BIT and UTF-
by
at 2011-05-26 03:07:27
original http://hlee.iteye.com/blog/1058790
ruby 1.9.2和mysql 2.8.1的encode不匹配问题
mysql 2.8.1有时候明明返回的是utf8但是却告诉ruby是ascii所以
所以,要么呢装一个好点版本的msyql,要么呢,如下?
lib/mysql_utf8.rb
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
在environments相关环境里load一下,就可以了
<br><br>
<span style="color:red">
<a href="http://hlee.iteye.com/blog/1058790#comments" style="color:red">已有 <strong>0</strong> 人发表留言,猛击->><strong>这里</strong><<-参与讨论</a>
</span>
<br><br><br>
ITeye推荐