I recently discovered a strange, intermittent/rare Encoding::CompatibilityError: incompatible character encodings: UTF-8 and ASCII-8BIT
error in almost all of our views.
I was able to reproduce it by sending a (possibly non-standard?) request such as curl 'http://rails.host.com:3000/?x=✓'
The problem boiled down to a strange cocktail that didn't play well:
- within one of the layouts, we used
request.fullpath
- this could produce a string which has#<Encoding:ASCII-8BIT>
encoding. Rarely, but it can happen. - within the footer layout, we had the
©
UTF-8 character.
So when this deadly cocktail was mixed, it produced the aforementioned error.
Now to my question: What's the best approach to resolve this?, ideally such that it would reduce the chances of re-occurrence
- option A: try to never use UTF-8 characters in the views (always HTML-encode all strings)
- option B: force
request.fullpath
to use UTF-8 encoding somehow - option C: something else I couldn't come up with
Both A,B seem rather brittle as solutions, and it's easy to miss doing those whenever views are updated or added. So I'm hoping for C...