Rails 3 - (incompatible character encodings: UTF-8 and ASCII-8BIT):

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

I find a lot of old information, but little advice about this error message, but wondered what the current status is, as it seems to be less discussed around the network. This happens to me when I try to display text from a locale file that includes accented characters like 'é'.

I am using rails 3.0.3, ruby ​​1.9.2 (and tried 1.8.7 with the same result), mysql2 adapter, utf8 encoding.

+4
source share
3 answers

, , Ruby .

, UTF-8, , .rb, :

# encoding: utf-8

UTF-8 :

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

, , , UTF-8 , encoding database.yml:

development:
  adapter: postgresql
  encoding: UTF8
  database: pg_development
  username: abe
  pool: 5
+3

, , "string".force_encoding ( "UTF-8" )

+2

Currently, this may be caused by issue in Mail 2.5.4, which "pollutes" the encoding of mail.

@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
+2
source

All Articles