又回到这个老问题,表单输入中文时数据库数据为乱码
解决方案: MySQL字符集采用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