错误:不兼容的字符编码:UTF-8和ASCII-8BIT

时间:2012-07-13 21:00:41

标签: ruby-on-rails oracle utf-8

我收到错误incompatible character encodings: UTF-8 and ASCII-8BIT,当数据库中的视图中出现一些字符时:ñ,á,é等。

我的环境是:

  • Rails:3.2.5
  • Ruby:1.9.4p194
  • 数据库:Oracle 10g(10.2.0.1.0)

我可以使用Toad将这些字符保存在数据库中。

我试着写这个,在我的观点的第一行:

<% # encoding: utf-8 %>

enviroment.erb

Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8

但没有解决这个问题。

拜托,有人可以给一些建议解决这个问题。

感谢。

2 个答案:

答案 0 :(得分:6)

我有同样的问题,经过数小时的猴子补丁搜索,我解决了这个问题。

    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

答案 1 :(得分:0)

在文件boot.rb中,我添加了这一行:

ENV['NLS_LANG'] = 'AMERICAN_AMERICA.UTF8'

惠特,我解决了我的问题。