rails 中文乱码
ruby 1.9.3
rails 3.2.6
mysql2
mysql记录里面包含中文字符的时候,在页面上显示乱码;尝试了在所有controller合model文件的第一行加上
# encoding: utf-8
在 .html.erb文件里面加
<% #encoding utf-8 %>
在 ApplicationController里面加上:
before_filter :set_charset def set_charset @headers["Content-Type"] = "text/html; charset=utf-8" end
将所有.erb.html文件编码改称utf-8
iconv -f ascii -t utf-8 xx.html.erb -o xx.html.erb
都没有起作用,
database.yml里面encoding 早就是 utf-8
感觉还是数据库编码不对;查了下:
mysql> show create database xxx;
+-------------+----------------------------------------------------------------------+ | Database | Create Database | +-------------+----------------------------------------------------------------------+ | xxx | CREATE DATABASE `xxx` /*!40100 DEFAULT CHARACTER SET latin1 */ | +-------------+----------------------------------------------------------------------+
果然,mysql默认的编码是latin1;因为还是开发中,直接drop database 然后:
create database xxx character set 'utf8';
然后就能正常显示中文了。
浙公网安备 33010602011771号