Rails Encoding in ASCII-8BIT

I know this has been asked a few times, but something strange happens for me:

I have an index view where rendering of certain characters (letters with an accent) causes Rails to raise an exception

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

so I checked the string encoding and it is actually ASCII-8BIT everywhere, although I set the correct encoding in UTF-8 in my .rb application

config.encoding = "utf-8"

and in my enviroment.rb

Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8

and in my database:

character_set_database = utf-8

as indicated in some manuals.

Lines are inserted with the textarea field and are not merged with any other already inserted line.

Weird things:

  • this only happens in the index view, whereas it doesn't happen in the show (same resource)
  • this happens only for this model (which is an email with an object and a body, but this should not affect anything)
  • , str.force_encoding('utf-8'), . (dev Ruby 2.0.0, Ruby 2.1.0, Rails4, MySql)
  • # encoding utf-8 .
  • str.force_encoding('ascii-8bit').encode('utf-8') Encoding::UndefinedConversionError "\xC3" from ASCII-8BIT to UTF-8, à, body.force_encoding('ascii-8bit').encode('UTF-8', :invalid => :replace, :undef => :replace, :replace => '?') ?, str.force_encoding('iso-8859-1').encode('utf-8') (a ?).

, : 2: - ascii-8bit? - ?

( rails4):

Rails

"\xC2" UTF-8 ASCII-8BIT UTF-8

UTF8 Ruby

:: UndefinedConversionError: "\xE4" ASCII-8BIT UTF-8

, .

+4
1

, , -, . :

some_string = "this is a string"

some_string = "" #empty string

, some_string, ASCII_8BIT, - :

some_string = some_string + unicode_string

. , ASCII-8BIT, , :

#encoding: utf-8

, .

, . , , - , , ASCII-8BIT, , UTF-8, , .

0

Source: https://habr.com/ru/post/1530587/


All Articles