views:

2573

answers:

2

Hi,

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 DB? or is a there a way to make them show?

Thanks,

Tam

+1  A: 

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
+1  A: 

I've come across this problem too. I noticed in my case that it is when I use the translation in the .yml with I18n. If the translation has a tilde it brings up the error. Like in "votación"... the error won't show if I spell the word without the tilde like in "votacion".

Any hints?

dangt85