7

I'm getting the following error with my Ruby 1.9 & Rails 2.3.4. This happens when user submits a non-ASCII standard character.

I read a lot of online resources but none seems to have a solution that worked.

I tried using (as some resources suggested)

string.force_encoding('utf-8') 

but it didn't help.

Any ideas how to resolve this? Is there a way to eliminate such characters before saving to the DB? Or, is a there a way to make them show?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Tam
  • 11,872
  • 19
  • 69
  • 119
  • 4
    There is a good post about it on Yehuda Katz blog: http://yehudakatz.com/2010/05/17/encodings-unabridged/ – makevoid May 20 '10 at 17:20
  • I've been fighting with this for a while. Mysql2 gem didn't work, nor any of the other solutions which were suggested. For other searchers who might stumble upon this: if you are using blob or some other data type in mysql, change it to string or text. It maintains encoding and no more headaches. – Damien Roche Nov 06 '12 at 13:56

2 Answers2

11

For ruby 1.9 and Rails 3.0.x, use the mysql2 adapter.

In your gemfile:

gem 'mysql2', '~> 0.2.7'

and update your database.yml to:

adapter: mysql2

http://www.rorra.com.ar/2010/07/30/rails-3-mysql-and-utf-8/

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Jhony Fung
  • 2,970
  • 4
  • 29
  • 32
  • 1
    Thanks, this worked great for me. But be warned that mysql2 v0.3.* throws errors with Rails 3.0.7. http://stackoverflow.com/questions/3467054/problem-with-mysql2-and-rails3-bundler – aNoble Jul 07 '11 at 20:47
  • 1
    I was struggling with the `incompatible character encodings: ASCII-8BIT and UTF-8` while using `rack-webconsole` — then I found this post. Thanks, @Jhony Fung! – Ben Kreeger Aug 10 '11 at 14:18
1

I don't know much about Ruby (or Rails), but I imagine the problem is caused by a lack of control over your character encodings.

First, you should decide which encoding you're storing in your database. Then, you need to make sure to convert all text to that encoding before storing in the database. In order to do that, you first need to know which encoding it is to begin with.

One often repeated piece of advice is to decode all input from whatever encoding it uses, to unicode (if your language supports it) as soon as possible after you get control of it. Then you know that all the text you handle in your program is unicode. On the other end, encode the text to whatever output-encoding you want as a last step before outputting it.

The key is to always know which encoding a piece of text is using at any given place in your code.

Epcylon
  • 4,674
  • 2
  • 26
  • 35