简体   繁体   English

ruby 1.9 + sinatra不兼容的字符编码:ASCII-8BIT和UTF-8

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

I'm trying to migrate a sinatra application to ruby 1.9 我正在尝试将sinatra应用程序迁移到ruby 1.9

I'm using sinatra 1.0, rack 1.2.0 and erb templates 我正在使用sinatra 1.0,机架1.2.0和erb模板

when I start sinatra it works but when I request the web page from the browser I get this error: 当我启动sinatra时,它可以工作,但是当我从浏览器请求网页时,出现此错误:

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

all .rb files has this header: 所有.rb文件都具有以下标头:

#!/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 我认为问题出在erb文件中,即使它表明它是UTF-8编码的

[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? sinatra不能与ruby 1.9完全兼容吗?

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, eg, input from an HTML form or data from a database. 我不熟悉您所处情况的细节,但是当尝试将源代码中的字符串(通常以UTF-8编码)与来自外部的字符串连接在一起时,在Ruby 1.9中会出现这种错误。系统,例如,来自HTML表单的输入或来自数据库的数据。

ASCII-8BIT is basically a synonym for binary. ASCII-8BIT基本上是二进制的同义词。 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). 这表明输入字符串没有使用实际使用的编码标记(例如,UTF-8或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. 我的理解是,在Ruby 1.8中看不到异常消息,因为它会将字符串视为二进制,并以静默方式连接不同编码的字符串。 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. 使错误消息消失的一种方法是对来自外部源的字符串使用force_encoding('UTF-8')(或其他某种编码)。 This is not to be done lightly, and you'll want to have a sense of the implications. 这不是轻描淡写,您需要对其中的含义有所了解。

I had the same issue. 我遇到过同样的问题。 The problem was a utf8 encoded file which should be us-ascii. 问题是utf8编码的文件应该是us-ascii。

I checked using the file command (on OSX): 我使用file命令(在OSX上)检查过:

$ file --mime-encoding somefile
somefile: utf-8

After removing the weird characters from the file: 从文件中删除奇怪的字符后:

$ file --mime-encoding somefile
somefile: us-ascii

This fixed the issue for me. 这为我解决了这个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails 2.3.5,Ruby 1.9,SQLite 3不兼容的字符编码:UTF-8和ASCII-8BIT - Rails 2.3.5, Ruby 1.9, SQLite 3 incompatible character encodings: UTF-8 and ASCII-8BIT 由于cookie的8位编码而导致错误“不兼容的字符编码:ASCII-8BIT和UTF-8”(Rails 3和Ruby 1.9) - Error 'incompatible character encodings: ASCII-8BIT and UTF-8' due to 8-bit encoding of cookies (Rails 3 and Ruby 1.9) 不兼容的字符编码:ASCII-8BIT 和 UTF-8 和 debug(params) - incompatible character encodings: ASCII-8BIT and UTF-8 and debug(params) Rails:geokit“不兼容的字符编码:UTF-8和ASCII-8BIT” - Rails: geokit “incompatible character encodings: UTF-8 and ASCII-8BIT ” 处理不兼容的字符编码:UTF-8和ASCII-8BIT - Dealing with incompatible character encodings: UTF-8 and ASCII-8BIT 不兼容的字符编码:渲染操作中的 UTF-8 和 ASCII-8BIT - incompatible character encodings: UTF-8 and ASCII-8BIT in render action 不兼容的字符编码:Oga gem中的ASCII-8BIT和UTF-8 - incompatible character encodings: ASCII-8BIT and UTF-8 in Oga gem 动作控制器:不兼容的字符编码:ASCII-8BIT和UTF-8? - Action Controller: incompatible character encodings: ASCII-8BIT and UTF-8? 不兼容的字符编码:ASCII-8BIT 和 UTF-8 - incompatible character encodings: ASCII-8BIT and UTF-8 使用ruby 1.9.1p429的rails 2.3.5:不兼容的字符编码:ASCII-8BIT和UTF-8 - rails 2.3.5 with ruby 1.9.1p429 : incompatible character encodings: ASCII-8BIT and UTF-8
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM