8

I moved a web app that was using 1.8.7 to 1.9.2 and now I keep getting

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

I have the database encoding to UTF-8 and I have also 'config.encoding = "utf-8"'.

I saw some ideas as possible workarounds and I added

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

But it didn't work either.

One specific chunk of code where I am getting this error is

%ul.address
- @user.address.split(',').each do |line|
  %li= line.titleize

I'm using HAML, I checked line.titleize, and the encoding is UTF-8. Seems that the template is being rendered with ASCII-8BIT and it gets screwed each time that I try to render characteres like 'ñ'

I'm working with Rails 3.0.5.

I have read the post by James Edward Gray, but I still can figure it out what is going on ;(.

I'd really appreciate any kind of help :D.

I also tried:

"string".force_encoding("UTF-8")

And

# encoding: utf-8

Without any luck.

Fixed


See comments.

tardate
  • 16,424
  • 15
  • 50
  • 50
Adolfo Builes
  • 501
  • 1
  • 3
  • 6
  • 2
    I've faced the same issue and it's usually a hidden string that's causing it. Let me ask you this, if you render without a layout (render :layout => false) and you remove all other code on the page except for the excerpt in your question, do you still get the same error? – Pan Thomakos Mar 22 '11 at 23:15
  • Hey Pan, thanks! if I do render :layout => false, it doesn't throw the error. – Adolfo Builes Mar 22 '11 at 23:46
  • 2
    Hey I just found where the problem was, I was using a value from the cookies in the template, turns out the cookies are ASCII-8BIT. cookies["location"].encoding # That is what was causing all the issues, I did force_encoding('UTF-8') and that solved it. Thanks Pan. – Adolfo Builes Mar 23 '11 at 00:07
  • Glad I could help :) The HAML error lines are often not accurate. – Pan Thomakos Mar 23 '11 at 01:23

2 Answers2

5

I just ran into something similar ... and found the fix hidden in the comments to this question, but think it is worth highlighting explicitly:

cookies are ASCII-8BIT but rails 3 templates are utf-8 by default. This means using a raw cookie value in a view may raise Encoding::CompatibilityError (if the user has an incompatible in the cookie value)

The fix (as noted by Adolfo Builes) is to coerce your cookie values to UTF-8, as in:

cookies["location"].force_encoding('UTF-8')
tardate
  • 16,424
  • 15
  • 50
  • 50
-1

for haml put

-# coding: UTF-8

line on the top left of the page.

Taryn
  • 242,637
  • 56
  • 362
  • 405
umitka
  • 69
  • 1
  • 1