Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby 1.9 + sinatra incompatible character encodings: ASCII-8BIT and UTF-8

I'm trying to migrate a sinatra application to ruby 1.9

I'm using sinatra 1.0, rack 1.2.0 and erb templates

when I start sinatra it works but when I request the web page from the browser I get this error:

Encoding::CompatibilityError at /
incompatible character encodings: ASCII-8BIT and UTF-8

all .rb files has this header:

#!/usr/bin/env ruby
# encoding: utf-8

I think the problem is in the erb files even if it shows that it's UTF-8 encoded

[user@localhost views]$ file home.erb
home.erb: UTF-8 Unicode text

any one had this problem before? is sinatra not fully compatible with ruby 1.9?

like image 981
John Avatar asked Jul 11 '10 15:07

John


1 Answers

I'm not familiar with the specifics of your situation, but this kind of error has come up in Ruby 1.9 when there's an attempt to concatenate a string in the source code (typically encoded in UTF-8) with a string from outside of the system, e.g., input from an HTML form or data from a database.

ASCII-8BIT is basically a synonym for binary. It suggests that the input string was not tagged with the actual encoding that has been used (for example, UTF-8 or ISO-8859-1).

My understanding is that exception messages are not seen in Ruby 1.8 because it treats strings as binary and silently concatenates strings of different encodings. For subtle reasons, this often isn't a problem.

I ran into a similar error yesterday and found this excellent overview.

  • http://yehudakatz.com/2010/05/05/ruby-1-9-encodings-a-primer-and-the-solution-for-rails/

One option to get your error message to go away is to use force_encoding('UTF-8') (or some other encoding) on the string coming from the external source. This is not to be done lightly, and you'll want to have a sense of the implications.

like image 121
Eric Walker Avatar answered Oct 21 '22 06:10

Eric Walker