I have some seed data in MySql which contains non-ASCII characters (as
you'd imagine as these are worldwide placenames that I'm storing). In
MySql on the command line they look great, they're all formatted
perfectly, including Arabic and Chinese characters and they look great.
However, when I ask my Rails model for one of these fields, and render
that in the browser, all the lovely work is undone, and I get garbage
character spew.
E.g.:
In the commandline, mySQL: "Чанчунь,长春" (Good!)
In the browser: "K’ua-ch’eng-tzu,Чанчунь,长春" (Bad!!)
So clearly this is an encoding issue, here's my setup:
Table created via migration with ":options => "DEFAULT CHARSET=utf8""
Database.yml contains "encoding: utf8"
HTML is served with the meta tag containing "charset=utf-8"
If I puts directly to the command line in the controller, I get the same
(bad) spew, so I don't think it's the browser screwing up. All I'm doing
to fetch the string is something like "@name = City.first.name"
I'm not sure how to inspect the HTTP headers as they're returned from
rails, without breaking out tcpdump what's the best way to see the
header Rails is creating?
Firebug reports the response header content-type as:
"Content-Type text/html; charset=utf-8"
So your HTTP headers are OK and the database is in the right encoding.
Now for my stupid question: have you got the text encoding in the
browser set to something other than auto-detect or UTF-8?
Best,
2. I learned that once created, each database, table, and column has a
charset assigned. And these were not automatically changed with the
change to my.cnf. To complete the fix, I would either have to do:
alter database db_name charset=utf8;
alter table t_name charset=utf8;
AND a similar alter for the columns.
Instead I just dropped everything and recreated the databases (which
then picked up the new charset) and it worked, finally. Wow, that was an
interesting bit of setup!