error ncompatible character encodings: UTF-8 and ASCII-8BIT

This is the full error anyone else having this issue?

incompatible character encodings: UTF-8 and ASCII-8BIT
activesupport (3.0.9) lib/active_support/core_ext/string/
output_safety.rb:82:in `concat'

Well I’m not having the same issue, but when I wanted to create a csv file in my controller and one of the cells contains

this -> ` rails is expecting something else, so I just add at the beggining of my controller the following

#encoding: utf8

and worked, maybe it helps

Javier

yeah this is actually in a view that the error is occurring. I have
tried to put this encoding statement in the view
but to no avail. hopefully someone has had this problem and can
instruct me . It seems
like this would be a common error.

The problem you're running into has to do with encoding mismatch
(under ruby 1.9) in some data you're munging/rendering in that view.
First thing you should do is check your config/setup regarding
encoding:
$ ./script/rails console...> __ENCODING__ => #<Encoding:UTF-8>

Encoding.default_internal => #<Encoding:UTF-8>
Encoding.default_external => #<Encoding:UTF-8>
ActiveRecord::Base.configurations[Rails.env]["encoding"] => "utf8" ...

Assuming your stack is properly setup for utf-8, then the most likely
culprit is that you have some non-utf-8 binary char(s) in some data
that is causing the problem (probably copy/pasted from ms-word into
some form).
Not the same error, but here's an example of a similar encoding-
related problem:

s = "foo™" => "foo™"
s.encoding

=> #<Encoding:UTF-8>

s.sub(/foo/, 'bar')

=> "bar™"

s2 = "foo™".force_encoding("ASCII")

=> "foo\xE2\x84\xA2"

s2.encoding

=> #<Encoding:US-ASCII>

s2.sub(/foo/, 'bar')

ArgumentError: invalid byte sequence in US-ASCII
...

You can test/log the underlying data you're trying to render in that
view to see what exactly is causing the problem in order to delete/
replace it.

The bigger issue is ... how to prevent and/or deal with this
particular case when it happens again? Probably many diff ways to
deal with this, but in such cases where I need to force the encoding
to be valid utf-8 for a given submitted string no matter what, I've
used something like the following:

  ...
  IC_UTF8 = Iconv.new('UTF-8//IGNORE', 'UTF-8')
  ...

  def force_utf8(str)
    #note added extra space plus chop to fix certain cases.
    IC_UTF8.iconv("#{str} ")[0..-2]
  end
  ...

s2

=> "foo\xE2\x84\xA2"

s2.encoding

=> #<Encoding:US-ASCII>

s3 = force_utf8(s2)

=> "foo™"

s3.encoding

=> #<Encoding:UTF-8>

s3.sub(/foo/, 'bar')

=> "bar™"

Jeff

I dont know if it will help you but if you are not using mysql2 gem I recommend you try it as a first shoot!

Regards
thiagocifani

Em Jan 1, 2012, às 10:38 PM, Jeff Lewis <jeff.burly@gmail.com> escreveu:hiafo