Error: Incompatible character encodings: UTF-8 and ASCII-8BIT

前端 未结 2 1121
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-05 14:21

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

My envir

相关标签:
2条回答
  • 2021-01-05 14:42

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

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

    Whit this I solved my problem.

    0 讨论(0)
  • 2021-01-05 14:45

    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 讨论(0)
提交回复
热议问题