2

I get this error when writing to database:

Encoding::UndefinedConversionError "\xD0" from ASCII-8BIT to UTF-8

After googling around a bit the problem seems to lie in ruby 1.9.2 string handling but no real solution found.

I use magic_encoding to force utf-8 on all data. My database runs on utf-8 as well.

I'm running rails 3.1 and ruby 1.9.2.

Anyone that can shine some light on this error?

pnuts
  • 58,317
  • 11
  • 87
  • 139
MattiasB
  • 135
  • 2
  • 12

1 Answers1

3

You should add this line to the top of your .rb file

# encoding: utf-8

Or you can use this gem

magic_encoding

Related topic:

Add "# coding: utf-8" to all files

Justin Tanner
  • 14,062
  • 17
  • 82
  • 103
fl00r
  • 82,987
  • 33
  • 217
  • 237
  • I use magic_encoding and I tried coding and encoding with no success. Any other suggestions? – MattiasB Oct 03 '11 at 08:49
  • that's weird. What the name of the file you are executing in this proces? Is that a rake task or just a model, or custom class or what? – fl00r Oct 03 '11 at 08:54
  • I read an excelfile and write down the content from a model class. – MattiasB Oct 03 '11 at 08:59
  • def upload_file(params) uploaded_io = params[:upload][:excelfile] File.open(Rails.root.join('public', 'uploadedfiles', uploaded_io.original_filename), 'w') do |file| file.write(uploaded_io.read) end end – MattiasB Oct 03 '11 at 08:59
  • In my very similar case adding `encoding` line fixes this problem. Last try: did you restart your app server? – fl00r Oct 03 '11 at 09:01
  • 3
    I found a solution. I used the method force_encoding and it magically worked. – MattiasB Oct 03 '11 at 09:22