Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
491 views
in Technique[技术] by (71.8m points)

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...