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

Hi,

did You find a solution? I have the same problem on all of my projects. DB in UTF8, rails in UTF8, but when it comes to partials with data with german umlauts I get the mentioned error.

As far as I understand it's a problem with the erb rendering engine that forces ASCII encoding.

This encoding-thing is so digusting...

Greetings Sven

forgot to say: a workaround that works for me is to add the method ".force_encoding('utf-8')" to any variable with date from my database eg.:

student.name.force_encoding('utf-8')

Good Night for now :slight_smile: S.

Gudleik Rasch wrote:

I've been struggling with the same issue a few times, and the solution that worked for me was to use a different mysql driver: ruby-mysql

# in config/environment.rb: config.gem 'ruby-mysql'

# or in Gemfile: gem 'ruby-mysql'

The encoding must also be set in config/database.yml: adapter: mysql encoding: utf8

Still not good night ... :slight_smile:

I tried with rails 3 (rc) and this adapter ruby-mysql and now everything works as expected!

Thanks a lot and now finally . Good Night!

S.

As you figured out, the problem is to do with the old 'mysql' gem not being encoding-aware for Ruby 1.9. (You're on Ruby 1.9, right?)

As an alternative to ruby-mysql, which is a little slow, you might want to try the 'mysql2' gem, which is an updated version of the original 'mysql' gem, and which has native extensions to hook into the MySQL C library, making it faster.

Chris

As an alternative to ruby-mysql, which is a little slow, you might want to try the 'mysql2' gem, which is an updated version of the original 'mysql' gem, and which has native extensions to hook into the MySQL C library, making it faster.

Chris

Hello Chris,

thank You very much for this hint! I'll give it a try in my next night-session..

greetings Sven

thank You very much for this hint! I'll give it a try in my next night-session..

greetings Sven

Oh my god.........

first of all, the mysql2 gem works fine - thanks for the hint!

But there's one more thing: I have lots of notices in my rails app like "... wurde gelöscht". But when I have one of those fckng german umlauts in my controller I get this wonderful character encodings error message.

I'll try to solve this with a locale and let You know...

greetings Sven

I'll try to solve this with a locale and let You know...

greetings Sven

hm, within a locale the german umlaute are allowed...

Hi, I began reading this post because i have the same problem:

Just start the aplicacion with new ruby 1.9.2 raises the error

"incompatible character encodings: ASCII-8BIT and UTF-8".....

But I have the problem before reading database, in the main menu when ruby read "opción" and "menú"

Does anybody this solution, it seems easy.

thanks

Sven Koesling wrote in post #935706:

thank You very much for this hint! I'll give it a try in my next night-session..

greetings Sven

Oh my god.........

first of all, the mysql2 gem works fine - thanks for the hint!

But there's one more thing: I have lots of notices in my rails app like "... wurde gelöscht". But when I have one of those fckng german umlauts in my controller I get this wonderful character encodings error message.

First: Display text does not belong in the controller. Keep it in the views (and perhaps the helpers).

Second: what is the encoding of the source file containing those characters?

Best,

Albert C. wrote in post #955265:

Hi, I began reading this post because i have the same problem:

Just start the aplicacion with new ruby 1.9.2 raises the error

"incompatible character encodings: ASCII-8BIT and UTF-8".....

But I have the problem before reading database, in the main menu when ruby read "opción" and "menú"

Does anybody this solution, it seems easy.

thanks

Hi Albert,

I don't understand this. Where are the words "opción" and "menú"? In the view there should be no problem.

greetings Sven

Hi, thanks for responding. It's very simple, before acces any database, I have a presentation menu, and a text in the view saying than user have to choose an option:

<div id="tit_menu">   <p>Seleccione una opción del menú</p> </div>

If I change "ó" with "o" and "ú" with "u" works fine.

It's not a satisfying solution but whenever I ran into this problem I added # Encoding: utf-8 to the top of the file causing this problem and it worked.

Would love to know how to get rid of all these comments and fix it for real though :slight_smile:

Hi,

Just for your information - switching to 'mysql2' gem solved my issue - I was storing Chinese in my DB..

Plus, mysql2 should be the default mysql adapter of Rails 3 anyways...

Hallo, I face a similar problem, but here mentioned solutions seem to not work.

When I try to display string with non-ascii chars from a mysql database in form, sometimes the program stops with the 'incompatible character encodings: ASCII-8BIT' error. This issue appears only at some fileds in a table! Configuration of all fields is the same (varchar-string or memo-text, encoding UTF-8).

For example: I have record with 4 string fields, all have the same content (the same word). When I try to display them, the 'incompatible...' error appears. When I check their '.encoding', I get UTF-8 for three and ASCII-8BIT for one! Sometimes, when I render this template again and again, two are ASCII and Two UTF. Strange - not? I dont know about any difference among these fields configuration.

Yes, .enforce-coding('utf-8') added to all instance in erb file works, but it is not natty solution.

The same problem appears both at old project transformed from Ruby 1.8/rails 2.2 and at new project creatd in 5minutes via scaffolding in ruby1,9,2/rails3.0.3.

The patch 'ruby_191_hacks.rb' does not work here, I got the same output as 'Posted by Heinz Strunk (cojones) on 2010-04-06 11:49'

I have realized, that the template (erb file) must be saved in the same coding, as the field are (i think the comment about UTF widely mentioned above says to interpeter, that the file is in UTF coding)

My configuration: Ruby 1.9.2, Rails 3.0.3 (3.0.1 behaves the same), mysql2 0.2.6 driver (mysql 2.8.1 makes the same problem, mysql-ruby does not work here - it misses an UNIX constant), both mysql2 and utf8 set in database.yml, machine WinXP SP3.

Any idea, how to induce ActiveRecord to provide all the fields in the UTF-8 encoding?

all, Thanks for good ideas, but like for Robert K. it's not working for me (ruby 1.9.2 and rails 3.0.3).

Robert K., check that your tables are of type = MyISAM and CHARACTER SET utf8 in Mysql.

I get the same error as you; the problem appears almost every time, but suddenly it works for again returning an error.

While we are waiting for a solution; how do you use encoding in the erb-files? -> "Yes, .enforce-coding('utf-8') added to all instance in erb file works" If you have the detail text in your file and the in witch erb-file do I have to put this encoding I would be very thankful.

Thanks again to all. Gjermund developer and DBA

Gjermund Lunder wrote in post #964675:

all, Thanks for good ideas, but like for Robert K. it's not working for me (ruby 1.9.2 and rails 3.0.3).

Robert K., check that your tables are of type = MyISAM and CHARACTER SET utf8 in Mysql.

Er, why MyISAM? That's generally a poor idea, since you don't get transactions or referential integrity.

(Of course, using MySQL itself is IMHO a poor idea...)

Best,

Because unfortunately innoDB tables don’t support full text searching of UTF-8 encoded content. But innoDB tables are more flexible in general. Source of information: MySQL :: MySQL 8.0 Reference Manual :: 12.10.5 Full-Text Restrictions and Ruby On Rails + UTF8 + Mysql | DotMana. This might not be relevant, but could be.

Robert K. (or others), If you do have have the exact line for the enforce coding and in witch erb files to put it I would be happy.

PS: I've tried <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> (Secure UTF-8 Input in Rails - igvita.com) in the top of my app/views/layout/application.html.erb I thought this would force the browser to use UFT-8. But not. I'm able to force the browser to show content after the page is read with right click- Encoding on screen (but only i just one of the string columns contains a special characters - strange).

Please quote when replying.

Gjermund Lunder wrote in post #964821:

Because unfortunately innoDB tables don’t support full text searching of UTF-8 encoded content.

I know that, but I didn't think full-text searching was at issue here. Perhaps I misread.

The fact that you have to choose is a major reason that I no longer use or recommend MySQL.

Best,

Hello,

I just tried to migrate one of my applications from ruby 1.8 to ruby 1.9

and get following error:

Showing app/views/layouts/application.html.erb where line #48 raised:

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

48: <%= render :partial => ‘layouts/menu’ %>

I added “<%# coding: utf-8 %>” in application.html.erb and

layouts/_menu.rhtml but it still throws that error, anyone knows why?

Hey, I would recommend using Ruby 1.9.2 instead of Ruby 1.9.1. Next,

mysql2 gems is not only faster than the ruby-mysql gem but it provides

the following benefits:

The Mysql2 gem is meant to serve the extremely common use-case of connecting, querying and iterating on results. Some database libraries out there serve as direct 1:1 mappings of the already complex C API’s available. This one is not.

It also forces the use of UTF-8 [or binary] for the connection [and all strings in 1.9, unless Encoding.default_internal is set then it’ll convert from UTF-8 to that encoding] and uses encoding-aware MySQL API calls where it can.

For more information, please see the following:

https://github.com/brianmario/mysql2

Good luck,

-Conrad

Anyone know how to add this patch?: 0001-enforced-utf-8-encoding-for-ruby-19-and-mysql-text.patch (#4683 ASCII-8BIT and UTF-8 in hell - Ruby on Rails - rails)

I've never applied such a pacth.

Thanks

Gjermund