原文: http://hideto.javaeye.com/blog/138874
又回到这个老问题,表单输入中文时数据库数据为乱码
解决方案: My SQL字符集采用utf8,创建数据库时也使用utf8,然后在application?.rb里加上:
# application.rb
before_filter :set_charset
before_filter :configure_charsets
def set_charset
@headers["Content-Type"] = "text/html; charset=utf-8"
end
def configure_charsets
@response.headers["Content-Type"] = "text/html; charset=utf-8"
suppress(ActiveRecord::StatementInvalid) do
ActiveRecord::Base.connection.execute 'SET NAMES UTF8'
end
end
这样就搞定了!