Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

incompatible character encodings: ASCII-8BIT and UTF-8 in Ruby 1.9

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?

like image 928
Tam Avatar asked Nov 22 '09 19:11

Tam


2 Answers

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/

like image 152
Jhony Fung Avatar answered Sep 21 '22 01:09

Jhony Fung


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.

like image 43
Epcylon Avatar answered Sep 23 '22 01:09

Epcylon