Rails 3 - (不兼容的字符编码:UTF-8和ASCII-8BIT):
不兼容的字符编码:UTF-8和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 'é'.
我使用的是rails 3.0.3,ruby 1.9.2(并且已经尝试过1.8.7同样的结果),mysql2适配器,utf8编码。
I'm using rails 3.0.3, ruby 1.9.2 (and have tried 1.8.7 with same result), mysql2 adapter, utf8 encoding.
当有一个编码不匹配,我的Ruby应用程序如何解析字符串以及如何数据库存储它们。
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.
为了在处理UTF-8时为自己修复这个问题,我确保我在这个.rb文件的顶部有这个:
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
或者,您可以使用这行:
Encoding.default_internal, Encoding.default_external = ['utf-8'] * 2
最后,我确保我的数据库通过在database.yml中设置 encoding
选项来内部使用UTF-8:
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