Rails 3 -(不兼容的字符编码:UTF-8 和 ASCII-8BIT):
不兼容的字符编码: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当存在 我的 Ruby 应用程序解析字符串的方式与数据库存储字符串的方式之间的编码不匹配。
为了在处理 UTF-8 时自行解决此问题,我确保将其置于相关 .rb 文件的顶部:
或者,您可以在应用程序配置文件中使用 < 全局设置默认 UTF-8 编码a href="http://gnuu.org/2009/11/02/ruby-1-9-encoding-issues-again/" rel="nofollow">这一行:
最后,我确定通过在database.yml中设置
encoding
选项,我的数据库在内部使用UTF-8: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:
Alternatively, you can globally set default UTF-8 encoding in your application config file with this line:
And finally, I make sure that my database is using UTF-8 internally by setting the
encoding
option in database.yml:我记得使用 "string".force_encoding("UTF-8") 解决过一次
I remember resolving this once by using "string".force_encoding("UTF-8")
目前,这可能是由 Mail 2.5.4 中的问题引起的,该问题“污染”邮件对象的编码。
For the time being, this can be caused by an issue in Mail 2.5.4, which 'pollutes' the encoding of the mail object.