ActionView::TemplateError (incompatible character encodings: ASCII-8BIT and UTF-

ruby 1.9.2和mysql 2.8.1的encode不匹配问题

mysql 2.8.1有时候明明返回的是utf8但是却告诉ruby是ascii所以

ActionView::TemplateError (incompatible character encodings: ASCII-8BIT and UTF-

所以,要么呢装一个好点版本的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一下,就可以了

你可能感兴趣的:(mysql,ITeye,Ruby)