# Character encoding problems

**URL:** <https://www.ruby-forum.com/t/character-encoding-problems/211527>\
**Category:** Rails\
**Created:** [September 6, 2011, 7:19pm UTC](https://www.ruby-forum.com/t/character-encoding-problems/211527 "2011-09-06T19:19:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [September 6, 2011, 7:19pm UTC](https://www.ruby-forum.com/t/character-encoding-problems/211527/1 "2011-09-06T19:19:28Z")

</div>

I’ve been fighting these problems since I moved to Ruby 1.9. I am now  
using Ruby 1.9.2 with Rails 2.3.11 and I am still having problems. The  
symptom is errors with this message:

(incompatible character encodings: UTF-8 and ISO-8859-1)

I’ve marked all my files with:

# -_- coding: utf-8 -_-

and I’ve tried doing everything I can to try and get everything to be  
UTF-8 but I still have other character sets – sometimes ISO-8859-1 as  
in this case, other ties it is ASCII-8BIT. These errors usually happen  
in the views as the various parts are being concatenated together.

I need a general approach to these problems. They are killing me and  
there doesn’t seem to be a clear way to deal with them.

Thank you,  
pedz

---

<div class="post-metadata">

**Author:** ![pedz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/839c29/32.png) [@pedz](https://www.ruby-forum.com/u/pedz)\
**Post date:** [September 6, 2011, 7:43pm UTC](https://www.ruby-forum.com/t/character-encoding-problems/211527/2 "2011-09-06T19:43:48Z")

</div>

Maybe it help

> **[Gray Soft / Not Found](http://graysoftinc.com/articles/ruby_19s_string)**
>
> Something has gone wrong.

Regards,  
Igor Escobar  
\*Software Engineer  
\*

- [http://blog.igorescobar.com](http://blog.igorescobar.com)
- [http://www.igorescobar.com](http://www.igorescobar.com)
- @igorescobar [http://www.twitter.com/igorescobar](http://www.twitter.com/igorescobar)

---

<div class="post-metadata">

**Author:** ![pedz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/839c29/32.png) [@pedz](https://www.ruby-forum.com/u/pedz)\
**Post date:** [September 6, 2011, 7:47pm UTC](https://www.ruby-forum.com/t/character-encoding-problems/211527/3 "2011-09-06T19:47:31Z")

</div>

Another good link:

> **[Working with Encodings in Ruby 1.9](https://nuclearsquid.com/writings/ruby-1-9-encodings/)**
>
> (Last updated on 11.2.2013)

Regards,  
Igor Escobar  
\*Software Engineer  
\*

- [http://blog.igorescobar.com](http://blog.igorescobar.com)
- [http://www.igorescobar.com](http://www.igorescobar.com)
- @igorescobar [http://www.twitter.com/igorescobar](http://www.twitter.com/igorescobar)

---

<div class="post-metadata">

**Author:** ![pedz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/839c29/32.png) [@pedz](https://www.ruby-forum.com/u/pedz)\
**Post date:** [September 6, 2011, 8:03pm UTC](https://www.ruby-forum.com/t/character-encoding-problems/211527/4 "2011-09-06T20:03:03Z")

</div>

Yes, I understand Ruby’s encodings. That isn’t my question.

My question is what is a good consistent way to deal with the in Rails  
given that the input may be any language, you do not have control over  
the various source files in the gems, etc.

i.e. since the rails files do not have the UTF-8 encoding tag, their  
defaults is whatever are the default is for my country. But that  
setting may be different from the end user’s browser’s settings. There  
are just all sorts of ways that different encodings can get into the  
mix. Hasn’t anyone else fought these wars besides me?

---

<div class="post-metadata">

**Author:** ![pedz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/839c29/32.png) [@pedz](https://www.ruby-forum.com/u/pedz)\
**Post date:** [September 6, 2011, 9:41pm UTC](https://www.ruby-forum.com/t/character-encoding-problems/211527/5 "2011-09-06T21:41:37Z")

</div>

Hi Perry,

I’ve run into such encoding issues under ruby 1.9.x and rails 3.x when  
dealing with unknown/untrusted/bad non-utf8 data params that need to  
be handled by the rails app as utf-8.

The fix I found (that works for the needs of my apps) was by following  
a strategy outlined by Paul B. ([http://po-ru.com/diary/fixing-](http://po-ru.com/diary/fixing-)  
invalid-utf-8-in-ruby-revisited/ ) using Iconv for forcing a string to  
utf-8:

…  
UTF8\_IC = Iconv.new(‘UTF-8//IGNORE’, ‘UTF-8’)

…  
def force\_utf8(v)  
return (v.is\_a?(String)) ? UTF8\_IC.iconv("#{v} ")[0…-2] : v  
end

…

and then adding a before\_filter which modifies submitted params  
strings forcing them to utf-8 before accessing/using those params in  
the app.

(Maybe there’s a better/simpler way since I last tested this issue?)

Jeff

---

<div class="post-metadata">

**Author:** ![pedz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/839c29/32.png) [@pedz](https://www.ruby-forum.com/u/pedz)\
**Post date:** [September 6, 2011, 11:26pm UTC](https://www.ruby-forum.com/t/character-encoding-problems/211527/6 "2011-09-06T23:26:03Z")

</div>

On Sep 6, 6:19pm, Perry S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> and I’ve tried doing everything I can to try and get everything to be  
> UTF-8 but I still have other character sets – sometimes ISO-8859-1 as  
> in this case, other ties it is ASCII-8BIT. These errors usually happen  
> in the views as the various parts are being concatenated together.
> 
> I need a general approach to these problems. They are killing me and  
> there doesn’t seem to be a clear way to deal with them.

I think Rails 3 is overall better at dealing with these encoding  
problems - I can’t say I’ve run into much in the way of encoding  
problems. I believe the mysql2 gem is better at ensuring that the data  
that comes out of the database is flagged as utf8

Fred

---

<div class="post-metadata">

**Author:** ![pedz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/839c29/32.png) [@pedz](https://www.ruby-forum.com/u/pedz)\
**Post date:** [September 6, 2011, 11:08pm UTC](https://www.ruby-forum.com/t/character-encoding-problems/211527/7 "2011-09-06T23:08:00Z")

</div>

Perry S. wrote in post #1020442:

> I’ve been fighting these problems since I moved to Ruby 1.9. I am now  
> using Ruby 1.9.2 with Rails 2.3.11 and I am still having problems. The  
> symptom is errors with this message:
> 
> (incompatible character encodings: UTF-8 and ISO-8859-1)
> 
> I’ve marked all my files with:
> 
> # -_- coding: utf-8 -_-

All that does is tell ruby that the strings in your source code should  
be interpreted as utf-8. Here is an example:

if data == “€” #That’s a euro symbol  
…

By default strings in ruby source code are ASCII.

> and I’ve tried doing everything I can to try and get everything to be  
> UTF-8 but I still have other character sets – sometimes ISO-8859-1 as  
> in this case, other ties it is ASCII-8BIT. These errors usually happen  
> in the views as the various parts are being concatenated together.
> 
> I need a general approach to these problems.

I don’t think there is one. The bottom line is you have to know the  
encoding of any  
data you read into your program. You can try to guess at an encoding,  
but whether that works in all cases, I don’t know
