7

I got the error incompatible character encodings: UTF-8 and ASCII-8BIT, when the view found in the database some characters like: ñ, á, é, etc.

My enviroment is:

  • Rails: 3.2.5
  • Ruby: 1.9.4p194
  • Database: Oracle 10g (10.2.0.1.0)

I can save this characters in the database, using Toad.

I tried to write this, in the first line of my view:

<% # encoding: utf-8 %>

In enviroment.erb

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

But nothing fixed this.

Please, can someone give some adviced to fixed this.

Thanks.

5
  • What is the database and national character set? select * from v$nls_parameter where parameter like '%CHARACTERSET'? Jul 13, 2012 at 21:01
  • As suggested in stackoverflow.com/questions/1779740/… you should read about and understand character encodings. The suggested links are a good place to start. Jul 16, 2012 at 13:31
  • thank @JustinCave for your answer, I gonna check this. Jul 22, 2012 at 0:49
  • @AdamHawkes thanks for your comment, I'll read the link for understand this. Jul 22, 2012 at 0:52
  • @JustinCave this is the sql's result: NLS_CHARACTERSER = WE8ISO8859P1 and NLS_NCHAR_CHARACTERSET = AL16UTF16 Jul 23, 2012 at 17:31

2 Answers 2

6

I have the same issues, and I resolved it after hours of searching with a monkey patch.

    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
0

In the file boot.rb, I added this line:

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

Whit this I solved my problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.