Converting your Rails app to utf8
Out the box, Rails sends utf8 content. However, the transport and database (in this case MySQL) most likely is *not* configured for utf8.
UTF8 being the fun beast that it is really needs to be used across the board from input to retrieval to prevent any nasty surprises. Besides, it’s good practice should you want to internationalize later.
The following outlines how to convert your Rails app to use utf8.
- Shutdown Rails
- Modify config/database.yml to add encoding: utf8 to each db instance
- Export database to dump file
- Convert the CHARSET from latin1 to utf8 in dump file
- Change MySQL configs and restart DB
- DROP and recreate DB
- Import DB via dump
- Start Rails
For each database,
- Â mysqldump —user=username —password=password —default-character-set=latin1 —skip-set-charset dbname > dump.sql
sed -e ‘s/CHARSET=latin1/CHARSET=utf8/g’ dump.sql
- AT THIS POINT STOP MYSQL, ADD THE OPTIONS BELOW AND THEN START MYSQL
- mysql —user=username —password=password —execute=”DROP DATABASE dbname; CREATE DATABASE dbname CHARACTER SET utf8 COLLATE utf8_general_ci;”
- mysql —user=username —password=password —default-character-set=utf8 dbname
- RESTART RAILS after making mods to database.yml.
MySQL
[mysqld]
# Set default charset
init-connect=’SET NAMES utf8’
init_connect=’SET collation_connection = utf8_general_ci’
default-character-set=utf8
default-collation=utf8_general_ci
[clients]
default-character-set=utf8
[mysql]
default-character-set=utf8
Rails
Put encoding: utf8 in database.yml
Nginx
charset utf-8
Some additional links: