Rails 3 -(不兼容的字符编码:UTF-8 和 ASCII-8BIT):

发布于 2024-10-07 16:15:26 字数 222 浏览 3 评论 0原文

不兼容的字符编码:UTF-8 和 ASCII-8BIT

我发现了很多旧信息,但关于此错误消息的建议很少,但想知道当前状态是什么,因为周围的讨论似乎较少网。当我尝试从包含重音字符(例如“é”)的语言环境文件中呈现文本时,就会发生这种情况。

我正在使用rails 3.0.3、ruby 1.9.2(并尝试过1.8.7,结果相同)、mysql2 适配器、utf8 编码。

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

I'm finding lots of old information yet scant advice about this error message but wondered what the current status is as there seems to be less discussion of it around the net. It occurs for me when I try to render text from a locale file that includes accented characters, for example 'é'.

I'm using rails 3.0.3, ruby 1.9.2 (and have tried 1.8.7 with same result), mysql2 adapter, utf8 encoding.

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

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

发布评论

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

评论(3

别忘他 2024-10-14 16:15:26

当存在 我的 Ruby 应用程序解析字符串的方式与数据库存储字符串的方式之间的编码不匹配

为了在处理 UTF-8 时自行解决此问题,我确保将其置于相关 .rb 文件的顶部:

# encoding: utf-8

或者,您可以在应用程序配置文件中使用 < 全局设置默认 UTF-8 编码a href="http://gnuu.org/2009/11/02/ruby-1-9-encoding-issues-again/" rel="nofollow">这一行:

Encoding.default_internal, Encoding.default_external = ['utf-8'] * 2

最后,我确定通过在database.yml中设置encoding选项,我的数据库在内部使用UTF-8:

development:
  adapter: postgresql
  encoding: UTF8
  database: pg_development
  username: abe
  pool: 5

I've gotten this error when there is an encoding mismatch between how my Ruby app is parsing strings and how the database stores them.

To fix this for myself when I'm dealing with UTF-8, I make sure I have this at the top of the .rb file in question:

# encoding: utf-8

Alternatively, you can globally set default UTF-8 encoding in your application config file with this line:

Encoding.default_internal, Encoding.default_external = ['utf-8'] * 2

And finally, I make sure that my database is using UTF-8 internally by setting the encoding option in database.yml:

development:
  adapter: postgresql
  encoding: UTF8
  database: pg_development
  username: abe
  pool: 5
平安喜乐 2024-10-14 16:15:26

我记得使用 "string".force_encoding("UTF-8") 解决过一次

I remember resolving this once by using "string".force_encoding("UTF-8")

夜司空 2024-10-14 16:15:26

目前,这可能是由 Mail 2.5.4 中的问题引起的,该问题“污染”邮件对象的编码。

@email = Email.find(1)
@email.body.encoding # This is a fresh instance from db, still okay
Mail.new(@email.body)
@email.body.encoding # value has been changed

For the time being, this can be caused by an issue in Mail 2.5.4, which 'pollutes' the encoding of the mail object.

@email = Email.find(1)
@email.body.encoding # This is a fresh instance from db, still okay
Mail.new(@email.body)
@email.body.encoding # value has been changed
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文