11

I am working on a rails app that submits a french translation via ajax and for some reason I keep getting the following error in the log:

Encoding::CompatibilityError incompatible character encodings: UTF-8 and ASCII-8BIT

Does anyone know how to fix this?

FIX:This works on the WEBrick sever

Place # encode: UTF-8 at the top of each file you want to work with different chars

I can't get this to work on a rails server with Thin... anyone else run into this?

dennismonsewicz
  • 25,132
  • 33
  • 116
  • 189

3 Answers3

2

https://rails.lighthouseapp.com/projects/8994/tickets/4336-ruby19-submitted-string-form-parameters-with-non-ascii-characters-cause-encoding-errors

the above link fixed my problem.

Specifically myString.force_encoding('UTF-8') on the string before sending it for translation.

Placed the sample code in the Application_controller.rb file and all is well

Nilloc
  • 845
  • 7
  • 20
dennismonsewicz
  • 25,132
  • 33
  • 116
  • 189
  • +1 for pasting the necessary code into the answer, would save others time – tmaximini Jul 12 '11 at 22:33
  • Code was on there for me, `String.force_encoding('UTF-8')` is the best workaround I've seen. Wasn't a problem until upgrading a server from 1.8.7 to 1.9.2 – Nilloc Mar 13 '12 at 23:01
0

I know this is old, but I had the same problem and the solution was in the link @dennismonsewicz gave. In detail, the code was:

was:

before_filter :force_utf8_params

  def force_utf8_params
    traverse = lambda do |object, block|
      if object.kind_of?(Hash)
        object.each_value { |o| traverse.call(o, block) }
      elsif object.kind_of?(Array)
        object.each { |o| traverse.call(o, block) }
      else
        block.call(object)
      end
      object
    end
    force_encoding = lambda do |o|
      o.force_encoding(Encoding::UTF_8) if o.respond_to?(:force_encoding)
    end
    traverse.call(params, force_encoding)
  end
danigb
  • 711
  • 6
  • 13
0

I fixed this issue by converting an utf8 file to ascii. See the answer here: ruby 1.9 + sinatra incompatible character encodings: ASCII-8BIT and UTF-8

Community
  • 1
  • 1
joost
  • 6,549
  • 2
  • 31
  • 36