在 ERB 中显示 UTF-8 表示“不兼容的字符编码” (不涉及数据库)
在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自 CSV#read 文档:
因此,为了安全起见,我会首先使用
并检查
在 ERB 模板中执行时紧接在
CSV#read
之后的 a) 和 b) 的内容。如果那里显示“US-ASCII”,那么您应该在解析后检查是否有某些内容意外地更改了您的国家/地区。至少你可以确定中间有什么奇怪的事情。我也更喜欢,
因为你已经有字符串了。但我认为这并不能解决问题。
我无法想象某些东西实际上改变了字符串的内容,但也许编码被重新关联为 US-ASCII。因此,您可能有一个很好的机会使用
您的字符串来强制关联回 UTF-8。如果这没有帮助,我会尝试
如果所有这些方法都失败,我需要知道在您尝试在 ERB 模板中呈现字符串时的编码,以进一步帮助您。
From CSV#read docs:
So just to be safe, I would first use
and check what
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
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
with your string in order to enforce the association back to UTF-8. If that doesn't help I'd try
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.
似乎您的 csv 文件未以 UTF-8 格式保存,请尝试将其转换为 utf-8
编辑:
我已经在本地 Mac 上使用相同的 ruby 版本测试了上面的示例,并且它可以正常工作。我从头开始创建了一个 csv 并在 irb 上进行了测试。
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.