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.
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”
What encoding is being set in the HTTP headers, if any?
What is the exact meta tag you are using?
The meta tag is:
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?
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?
The Firebug plugin to Firefox works pretty well. Or just use telnet
from the command line.
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?
The Firebug plugin to Firefox works pretty well. Or just use telnet
from the command line.
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?
The Firebug plugin to Firefox works pretty well. Or just use telnet
from the command line.
curl -I http://…/
is my favorite. Only prints back the headers the server sends and not
the body…
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,
Bingo! Thanks Hassan, most of my entries are ‘latin1’.
However I tried to set them with the set command (e.g., “set
character_set_results = utf8;”), that didn’t have any effect so I
rebooted mysql and the settings went back to latin1 again.
I’m Googling furiously for the answer, but is there a place where I can
set these values? My.cnf?
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!
Thanks to all for the help.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.