4
votes

I'm trying to make my Rails application UTF-8 native. I'm doing this because since upgrading to Ruby 1.9, I've run into this issue, where everytime my web page has to display some UTF-8 characters, I get:

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

as an error page. I've diagnosed that the error comes when I try to render any UTF-8 text, as this sort of web page would fail:

 %h3.book_title
  = "カタカナ"

(Asking for the encoding on the above string would return UTF-8)

Yet this one would succeed:

 %h3.book_title
  = "カタカナ".force_encoding("ASCII-8BIT")

So it looks like Rails is deciding to render all pages since the 1.9 upgrade with ASCII-8BIT encoding by default. This is a problem, since my page frequently presents multi-language data that only works well in UTF-8.

Things I have tried:

I'm using the Mysql2 gem. I've put this in my environment.rb:

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

This is in my application.rb:

config.encoding = "utf-8"

None of this is really working. Can anyone shed any light on how to make my application only use UTF-8 instead of this ASCII-8BIT nonsense?

1
How is your database set up when it comes to encodings?froderik

1 Answers

-2
votes

Add the magical character encoding shebang as the first line in your view file to force UTF:

# encoding: UTF-8
%h3.book_title
    = "カタカナ"