Kieran Klaassen
Kieran Klaassen

Reputation: 2202

Rails serialize gives wrong characters

I have a class with a field that uses serialize but found that some of my text get strange characters after inputting normal text. Is this something with encoding? How do I fix this?

I use Heroku with their postgresql database. When doing it in SQLite it works like a charm.

SQlite:

Ik zeg wat ik voel.

 Doe ik ...

Postgres:

Ik zeg wat ik voel.â¨â¨ Doe ik ...

Thanks!

Upvotes: 0

Views: 131

Answers (2)

Esailija
Esailija

Reputation: 140210

In the postgres you are interpreting UTF-8 bytes in ISO-8859-1. Given that in your post, there are 2x U+2028 line separators, whose UTF-8 representation is 0xE2 0x80 0xA8, which shows up like ⨠when interpreted in ISO-8859-1, with the 0x80 in the middle being invisible control character that was not transmitted to your post.

So the issue is whatever you are viewing the result in, is getting the correct bytes for UTF-8, but interpreting them as ISO-8859-1.

Upvotes: 0

rorra
rorra

Reputation: 9693

First check that your postgresql and your system encoding its UTF-8 (the safest), also, check that on config/database.yml you have setup the encoding UTF-8 for the postgres connection.

If that doesn't fix the issue, then change the yaml engine to syck, on your file config/boot.rb write

require 'yaml'
YAML::ENGINE.yamler = 'syck' 

that should fix the weird characters.

Upvotes: 1

Related Questions