1

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...

gingerlime
  • 5,206
  • 4
  • 37
  • 66
  • Hi, I'm running into the exact same issue. Did you get yours resolved? If so, can you post it as an answer pls? – membLoper Dec 06 '13 at 11:14
  • I kinda got it resolved, by excluding UTF-8 characters from the views and only using urlencoded ascii strings. However, I'm not sure it's the best approach or something easy to stick to longer-term...(hence my question) – gingerlime Dec 06 '13 at 13:04
  • YWIW, I posted my question here and found what caused the problem in my case http://stackoverflow.com/questions/20346335/rails-view-encoding-issues/20558455#20558455 – membLoper Dec 13 '13 at 03:16
  • Thanks. Yes, that's one possible solution (Option B). I'm just trying to figure out the most easy to use approach in general. Those two options will solve the problem, but it's very easy to incorporate an element that might not be UTF-8, and in future it will break. Or do you `force_encoding('UTF-8')` on every string? Once it breaks the error messages aren't particularly helpful pinpointing the problem... This is why I hope for a 'best practice' or a more solid / longer-term solution. – gingerlime Dec 14 '13 at 09:08

0 Answers0