Home » Ruby » incompatible character encodings: ASCII-8BIT and UTF-8

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

Posted by: admin November 30, 2017 Leave a comment

Questions:

I use Ruby 1.9.2 and Rails 3.0.5

I have the following error:

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

It has nothing to do with the database i think.

The error is happinging on this line in a view (just a div haml call):

#content

full stack:

My gems:

Answers:

I have a suspicion that you either copy/pasted a part of your Haml template into the file, or you’re working with a non-Unicode/non-UTF-8 friendly editor.

See if you can recreate that file from the scratch in a UTF-8 friendly editor. There are plenty for any platform and see whether this fixes your problem. Start by erasing the line with #content and retyping it manually.

Questions:
Answers:

I solved it by following these steps:

  • Make sure config.encoding = "utf-8" is in the application.rb file.
  • Make sure you are using the ‘mysql2’ gem.
  • Put # encoding: utf-8 at the top of file containing UTF-8 characters.
  • Above the <App Name>::Application.initialize! line in the environment.rb file, add following two lines:

http://rorguide.blogspot.com/2011/06/incompatible-character-encodings-ascii.html

Questions:
Answers:

Try to find the exact line which causing this problem and then enforce UTF8 coding, this solution worked for me.

title.to_s.force_encoding(“UTF-8”)

Questions:
Answers:

ASCII-8BIT is Ruby’s description for characters above the normal 0-0x7f ASCII character-set, and that are single-byte characters. Typically that would be something like ISO-8859-1, or one of its siblings.

If you can identify which character is causing the problem, then you can tell Ruby 1.9.2 to convert between the character set of that character to UTF-8.

James Grey wrote a series of blogs talking about these sort of problems and how to deal with them. I’d recommend going through them.

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

That typically happens because you are trying to concatenate two strings, and one contains characters that do not map to the character-set of the other string. There are characters in ISO-8859-1 that do not have equivalents in UTF-8, and vice-versa and how to handle string joining with those incompatibilities requires the programmer to step in.

Questions:
Answers:

I installed gem package mysql2.

and then I changed the adapter in mysql2 in database.yml.

Questions:
Answers:

For Haml put an encoding hint:

on the top left of the Haml page.

Questions:
Answers:

The problem was the use of incorrect quotes around the iOS version. Make sure all your quotes are ‘ and not ‘ or ’.

https://github.com/CocoaPods/CocoaPods/issues/829

Questions:
Answers:

you can force UTF8 with force_encoding(Encoding::UTF_8):

Example:

Questions:
Answers:

I’ve experienced similar problem. Although I had have UTF-8 encodings solved (with mysql2 and Encoding.default_external = Encoding::UTF_8 …) incompatible character encodings: UTF-8 and ASCII-8BIT arose when I used incorrect helper parameters e.g. f.button :submit, "Zrušiť" – works perfectly but f.button "Zrušiť"– throws encoding error.

Questions:
Answers:

I encountered the error while migrating an app from Ruby 1.8.7 to 1.9.3 and it only occured in production. It turned out that I had some leftovers in my Memcache store. The now encoding sensitive Ruby 1.9.3 version of my app tried to mix old ASCII-8BIT values with new UTF-8.

It was as simple as flushing the cache to fix it for me.

Questions:
Answers:

I had a similar issue on a custom CoffeeScript file. I solved it by changing the endline encoding from “Unix/Linux” to “Mac OS Classic”

Questions:
Answers:

The creation of pdf-documents with the rails-latex-gem lead to a similar problem.
I solved this by modifying layouts/application.pdf.erb to

Questions:
Answers:

it’s very strange that I met this problem because I forgot specify the ‘type’ parameter. e.g.:

which should be:

Questions:
Answers:

Just for the record: for me it turned out that it was the gem called ‘mysql’ … obviously this is working with US-ASCII 8 bit by default. So changing it to the gem called mysql2 (the 2 is the important point here) solved all of my issues.

I looked @ the gem list posted above – Michael Koper has obviously mysql2 installed but I posted this in case someone has this issue as well .. (took me some time to figure out).

If you dislike this answer please comment and I will delete it.

P.S: German umlauts (ä,ö and ü) screwed it out with mysql

Questions:
Answers:

I had the same problem when parsing CSV files on Ruby 1.9.2 that were correctly parsed on Ruby 1.8. I found the answer here. When opening the CSV file with Ruby CSV module it is necessary to specify UTF-8 enconding as following:

Questions:
Answers:

i had a similiar problem and the gem string-scrub automagically fixed it for me. https://github.com/hsbt/string-scrub If the given string contains an invalid byte sequence then that invalid byte sequence is replaced with the unicode replacement character (�) and a new string is returned.

Questions:
Answers:

I got the same cryptic error message from Rails 4.1, Ruby 2.3.3 in a recent project, stacktrace originating in layout application.html.haml

After a wild goose chase, the culprit was a UTF-8 character which recently had been added to the footer of all pages. For some weird reason the error would only show up intermittently.

Replacing the UTF-8 character with the corresponding HTML escape sequence &#xHHHH; solved the issue.

I hope this saves other people some time in the future..

Questions:
Answers:

For prevent an error “can’t modify frozen string” for encoding a varible you can use: var.dup.force_encoding(Encoding::ASCII_8BIT) or var.dup.force_encoding(Encoding::UTF_8)