4

incompatible character encodings: UTF-8 and ASCII-8BIT

I'm finding lots of old information yet scant advice about this error message but wondered what the current status is as there seems to be less discussion of it around the net. It occurs for me when I try to render text from a locale file that includes accented characters, for example 'é'.

I'm using rails 3.0.3, ruby 1.9.2 (and have tried 1.8.7 with same result), mysql2 adapter, utf8 encoding.

mark
  • 10,316
  • 6
  • 37
  • 58
  • 3
    I can't answer for Ruby but basically there is no such thing as 8-bit ASCII. http://en.wikipedia.org/wiki/ASCII is a 7-bit encoding with the bit never set. "é" exists in *nix 8-bit encodings such as http://en.wikipedia.org/wiki/ISO/IEC_8859-1, Windows encodings such as http://en.wikipedia.org/wiki/Windows-1252, and Apple Macintosh encodinds such as http://en.wikipedia.org/wiki/Mac_OS_Roman. It's in a different position in MacRoman than the other two and requires two bytes in UTF-8. – hippietrail Dec 17 '10 at 11:38
  • Do you have the copy of the original email that caused this? I'm running into something similar and would love to have a good test email (for reasons more complex than I care to explain, I can't get the one that is failing for me) – jwg2s Aug 07 '14 at 15:38

3 Answers3

3

I've gotten this error when there is an encoding mismatch between how my Ruby app is parsing strings and how the database stores them.

To fix this for myself when I'm dealing with UTF-8, I make sure I have this at the top of the .rb file in question:

# encoding: utf-8

Alternatively, you can globally set default UTF-8 encoding in your application config file with this line:

Encoding.default_internal, Encoding.default_external = ['utf-8'] * 2

And finally, I make sure that my database is using UTF-8 internally by setting the encoding option in database.yml:

development:
  adapter: postgresql
  encoding: UTF8
  database: pg_development
  username: abe
  pool: 5
Abe Voelker
  • 30,124
  • 14
  • 81
  • 98
  • In the end my problem was caused by static I18n files which held binary data placed there by some plugin web interface I used for managing translation. Look for things like day: !binary | RMOtYQ== in your i18n files. – mark Jun 22 '11 at 18:35
2

For the time being, this can be caused by an issue in Mail 2.5.4, which 'pollutes' the encoding of the mail object.

@email = Email.find(1)
@email.body.encoding # This is a fresh instance from db, still okay
Mail.new(@email.body)
@email.body.encoding # value has been changed
prusswan
  • 6,853
  • 4
  • 40
  • 61
2

I remember resolving this once by using "string".force_encoding("UTF-8")

Fareesh Vijayarangam
  • 5,037
  • 4
  • 23
  • 18