错误:不兼容的字符编码:UTF-8和ASCII-8BIT
我得到了错误incompatible character encodings: UTF-8 and ASCII-8BIT
,当在数据库中找到一些字符时,如:ñ,á,é等。
我的环境是:
- Rails:3.2.5
- Ruby:1.9.4p194
- 数据库:Oracle 10g(10.2.0.1.0)
我可以使用Toad将这些字符保存在数据库中。
我试图在我的观点的第一行写这个:
在enviroment.erb
Encoding.default_external = Encoding::UTF_8 Encoding.default_internal = Encoding::UTF_8
但没有解决这个问题。
请,有人可以给一些建议来解决这个问题。
谢谢。
我有同样的问题,经过数小时的猴子补丁搜索,我解决了这个问题。
module ActiveSupport #:nodoc: class SafeBuffer < String def safe_concat(value) value = force_utf8_encoding(value) raise SafeConcatError unless html_safe? original_concat(value) end def concat(value) value = force_utf8_encoding(value) if !html_safe? || value.html_safe? super(value) else super(ERB::Util.h(value)) end end alias << concat private def force_utf8_encoding(value) self.force_encoding('UTF-8').html_safe unless self.encoding.name == 'UTF-8' value = (value).force_encoding('UTF-8').html_safe unless value.nil? || value.encoding.name == 'UTF-8' value end end end
在文件boot.rb
,我添加了这一行:
ENV['NLS_LANG'] = 'AMERICAN_AMERICA.UTF8'
惠特,我解决了我的问题。