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

5

我在生产环境中遇到了不兼容的字符编码错误。我尝试在本地重现它,但没有成功。以下是错误信息:

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"。令我困惑的是,这个问题只在Passenger重新启动后的一段时间内存在,并且在一段时间后消失。进一步调试异常回溯给了我以下信息:

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 (incompatible character encodings: UTF-8 and ASCII-8BIT)"中提到了ActionDispatch::Request可能会导致问题,但是我不知道为什么问题会在一段时间后自行解决!

2个回答

1

如果您在使用来自数据库的任何文本时遇到此错误,则需要在database.yml中使用mysql2代替mysql适配器。同时,在Gemfile中添加mysql2 gem也是必须的。这将解决您的问题。


0

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


1
我们已经强制使用UTF-8编码了,它也奏效了。但是,仍然无法找出之前问题是如何自行解决的。 - user1864666

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接