在 ERB 中显示 UTF-8 表示“不兼容的字符编码” (不涉及数据库)

发布于 2024-11-25 18:34:37 字数 573 浏览 2 评论 0原文

在我的 ERB 页面中,我想显示此 CSV 文件中配置的国家/地区名称之一:

Suisse
Deutschland
日本

这是在 config/initializers 中加载 CSV 的代码:

require 'csv'
COUNTRIES = CSV.read("#{RAILS_ROOT}/config/countries.csv").flatten

这是 ERB 中的代码:

<%= "Country:" + COUNTRIES[id].to_s %>

我显示当 id=0 或 id=1 时没问题,但当 id=2 时出现错误:

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

...错误指向上面的 ERB 行。
如何修复它?

不涉及数据库,Ruby 1.9.2-p180。本地化文件中的 UTF-8 显示正常。

In my ERB page, I want to display one of the country names configured in this CSV file:

Suisse
Deutschland
日本

Here is the code that loads the CSV in config/initializers:

require 'csv'
COUNTRIES = CSV.read("#{RAILS_ROOT}/config/countries.csv").flatten

Here is the code in the ERB:

<%= "Country:" + COUNTRIES[id].to_s %>

I displays fine when id=0 or id=1, but when id=2 an error appears:

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

...with the error pointing to the ERB line above.
How to fix it?

No database involved, Ruby 1.9.2-p180. UTF-8 from localization files is displayed fine.

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

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

发布评论

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

评论(2

玩物 2024-12-02 18:34:37

来自 CSV#read 文档:

此方法还了解一个附加的 :encoding 参数,您可以使用该参数指定要读取的文件中数据的编码。除非您的数据位于 Encoding::default_external() 中,否则您必须提供此信息。 CSV 将使用它来确定如何解析数据。

因此,为了安全起见,我会首先使用

COUNTRIES = CSV.read("/Users/dumitru/test.csv", { encoding: "UTF-8" }).flatten

并检查

COUNTRIES[2].encoding.name

在 ERB 模板中执行时紧接在 CSV#read 之后的 a) 和 b) 的内容。如果那里显示“US-ASCII”,那么您应该在解析后检查是否有某些内容意外地更改了您的国家/地区。至少你可以确定中间有什么奇怪的事情。

我也更喜欢,

<%= "Country:" + COUNTRIES[id] %>

因为你已经有字符串了。但我认为这并不能解决问题。

我无法想象某些东西实际上改变了字符串的内容,但也许编码被重新关联为 US-ASCII。因此,您可能有一个很好的机会使用

COUNTRIES[2].force_encoding("UTF-8")

您的字符串来强制关联回 UTF-8。如果这没有帮助,我会尝试

COUNTRIES[2].encode("UTF-8")

如果所有这些方法都失败,我需要知道在您尝试在 ERB 模板中呈现字符串时的编码,以进一步帮助您。

From CSV#read docs:

This method also understands an additional :encoding parameter that you can use to specify the Encoding of the data in the file to be read. You must provide this unless your data is in Encoding::default_external(). CSV will use this to deterime how to parse the data.

So just to be safe, I would first use

COUNTRIES = CSV.read("/Users/dumitru/test.csv", { encoding: "UTF-8" }).flatten

and check what

COUNTRIES[2].encoding.name

says a) directly after CSV#read and b) when executed in your ERB template. If it says "US-ASCII" there then you should check whether something accidentally changes your COUNTRIES after parsing them. At least you can then be sure that something's odd inbetween.

I would also prefer

<%= "Country:" + COUNTRIES[id] %>

since you already have strings there. But I don't think this will cure the issue, though.

I couldn't imagine that something actually changed the contents of the string but maybe the encoding got reassociated as US-ASCII. So you might have a good chance to use

COUNTRIES[2].force_encoding("UTF-8")

with your string in order to enforce the association back to UTF-8. If that doesn't help I'd try

COUNTRIES[2].encode("UTF-8")

If all of these methods fail, I would need to know the encoding at the time you try to render the string in your ERB template to help you further.

森罗 2024-12-02 18:34:37

似乎您的 csv 文件未以 UTF-8 格式保存,请尝试将其转换为 utf-8

编辑:
我已经在本地 Mac 上使用相同的 ruby​​ 版本测试了上面的示例,并且它可以正常工作。我从头开始创建了一个 csv 并在 irb 上进行了测试。

ruby-1.9.2-p180 :004 > require 'csv'
=> true  
ruby-1.9.2-p180 :006 > COUNTRIES = CSV.read("/Users/dumitru/test.csv").flatten
=> ["Suisse", "Deutschland", "\xE6\x97\xA5\xE6\x9C\xAC"] 
ruby-1.9.2-p180 :007 > "Country:" + COUNTRIES[0].to_s
=> "Country:Suisse" 
ruby-1.9.2-p180 :009 > "Country:" + COUNTRIES[2].to_s
=> "Country:\xE6\x97\xA5\xE6\x9C\xAC" 

seems that your csv file isn't saved in UTF-8, try to convert it to utf-8

edit:
I've tested the above example on my local mac, using the same ruby version and it works without issues. I've created a csv from scratch and tested on irb.

ruby-1.9.2-p180 :004 > require 'csv'
=> true  
ruby-1.9.2-p180 :006 > COUNTRIES = CSV.read("/Users/dumitru/test.csv").flatten
=> ["Suisse", "Deutschland", "\xE6\x97\xA5\xE6\x9C\xAC"] 
ruby-1.9.2-p180 :007 > "Country:" + COUNTRIES[0].to_s
=> "Country:Suisse" 
ruby-1.9.2-p180 :009 > "Country:" + COUNTRIES[2].to_s
=> "Country:\xE6\x97\xA5\xE6\x9C\xAC" 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文