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

发布于 2024-09-10 02:23:54 字数 529 浏览 3 评论 0原文

我正在尝试将 sinatra 应用程序迁移到 ruby​​ 1.9

我正在使用 sinatra 1.0、rack 1.2.0 和 erb 模板,

当我启动 sinatra 时,它可以工作,但是当我从浏览器请求网页时,我收到此错误:

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

all .rb文件有这个标题:

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

我认为问题出在 erb 文件中,即使它显示它是 UTF-8 编码的,

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

以前有人遇到过这个问题吗? sinatra 与 ruby​​ 1.9 不完全兼容吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑饮青盏花 2024-09-17 02:23:54

我不熟悉您的情况的具体情况,但是当尝试将源代码中的字符串(通常以 UTF-8 编码)与来自外部的字符串连接时,Ruby 1.9 中就会出现这种错误。系统,例如来自 HTML 表单的输入或来自数据库的数据。

ASCII-8BIT 基本上是二进制的同义词。它表明输入字符串未使用已使用的实际编码进行标记(例如,UTF-8 或 ISO-8859-1)。

我的理解是,在 Ruby 1.8 中看不到异常消息,因为它将字符串视为二进制并默默地连接不同编码的字符串。由于微妙的原因,这通常不是问题。

我昨天遇到了类似的错误,并发现了这个出色的概述。

让错误消息消失的一种选择是对来自外部源的字符串使用force_encoding('UTF-8')(或其他一些编码)。这不是一件容易的事,你需要了解其中的含义。

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.

凉宸 2024-09-17 02:23:54

我有同样的问题。问题是 utf8 编码的文件应该是 us-ascii。

我使用 file 命令(在 OSX 上)进行了检查:

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

从文件中删除奇怪的字符后:

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

这解决了我的问题。

I had the same issue. The problem was a utf8 encoded file which should be us-ascii.

I checked using the file command (on 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文