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



我在生产中遇到一个不兼容的字符编码错误。我试图在当地复制它,但没有成功。这是错误消息:

A ActionView::Template::Error occurred in controller_name#action_name:
 incompatible character encodings: UTF-8 and ASCII-8BIT
 activesupport (3.0.5) lib/active_support/core_ext/string/output_safety.rb:80:in `concat'

config.encoding在application.rb中已经设置为"utf-8"。令我困惑的是,这个问题只在乘客重新启动前后持续了一段时间,一段时间后就消失了。对异常回溯的进一步调试给了我以下信息:

activesupport (3.0.5) lib/active_support/core_ext/string/output_safety.rb:80:in `concat'
activesupport (3.0.5) lib/active_support/core_ext/string/output_safety.rb:80:in `concat'
actionpack (3.0.5) lib/action_view/template/handlers/erb.rb:14:in `<<'
app/views/web/controller_name/action_name.erb:98
<%currentUrl = request.url%>
Line 98: <a href="<%= raw currentUrl %>" id="xyz">

"ActionView::Template::Error(不兼容的字符编码:UTF-8和ASCII-8BIT)"中提到,ActionDispatch::Request可能会导致此问题,但我不知道该问题如何在一段时间后自行解决!

如果您在来自数据库的任何文本中都收到了这个错误,那么您需要在database.yml中使用mysql2而不是mysql适配器。并在Gemfile中添加mysql2-gem。这将解决你的问题。

在文件顶部添加# encoding: UTF-8,尝试将文件编码设置为UTF-8。

相关内容

最新更新