Properly handling unicode characters in Rails
Posted
by Gdeglin
on Stack Overflow
See other posts from Stack Overflow
or by Gdeglin
Published on 2010-03-10T00:19:28Z
Indexed on
2010/05/24
5:51 UTC
Read the original article
Hit count: 452
By default Rails allows users of our application to input non-utf8 data, such as: ¶®«¼
However when we attempt to retrieve the data from our database and render it in a template Rails incorrectly assumes that it is in UTF-8 format and throws an error.
ArgumentError: invalid byte sequence in UTF-8
What is the best way to handle this? I have seen one fix that suggested sanitizing the data in every place the user can input it. However, that would involve changing a considerable amount of code and it would strip out the characters entirely. Ideally we would want some characters converted to their UTF-8 equivalents.
Our environment:
Ruby: 1.9.1
Rails 2.3.5
MySql Gem: 2.8.1
This is a serious and urgent problem for us so your answers are very appreciated!
© Stack Overflow or respective owner