incompatible character encodings: UTF-8 and ASCII-8BIT/2011-11-13 Askru post

from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
< incompatible character encodings: UTF-8 and ASCII-8BIT
Revision as of 12:28, 4 February 2020 by Woozle (talk | contribs) (Created page with "[https://stackanswers.net/questions/actionview-template-error-incompatible-character-encodings-utf-8-and-ascii-8bit This post] on stackanswers.net aka Askru may offer some clu...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This post on stackanswers.net aka Askru may offer some clues. I've translated the first answer, which seems useful, from Russian with Google Translate.

Translation

After doing some debugging, I found out that the problem occurs when using ActionDispatch :: a request object that happens to have strings that are all encoded in ASCII-8bit, regardless of whether my application is encoded in UTF-8 or not. I don’t know why this only happens when using a production server on Linux, but I will assume that this is some kind of ruby, or Rails, since I could not reproduce this error locally. The error occurred precisely because lines like this:

@current_path = request.env['PATH_INFO']

When this instance variable was printed in the Haml template, it caused an error because the string was encoded in ASCII-8bit instead of UTF-8. To solve this problem, I did the following:

@current_path = request.env['PATH_INFO'].dup.force_encoding(Encoding::UTF_8)

Which forced @current_path to use a duplicate string that was forced into the proper UTF-8 encoding. This error may occur with other data related to a similar request.headers request.