9

I am using rails 2.3.3 and ruby 1.9.1.

I am trying to render a view that includes a partial. In the partial i output a field of a model that is encoded in UTF8. This fails with

ActionView::TemplateError (incompatible character encodings: ASCII-8BIT and UTF-8) on line #248 of app/views/movie/show.html.erb:
245:    <!-- Coloumn right | start -->
246:    <div class="col_right">
247: 
248:        <%= render :partial => 'movie_stats' %>
249: 
250:        <!-- uploaders -->
251:        <div class="box_white">     

On the other hand, i can output the field with utf8 content just fine if i directly use that field in a view (when it is not in a partial).

How can i fix this? I already tried setting the default encoding but that did not seem to work.

1
  • How did you try setting the default encoding? Can you give us an example?
    – Ryan Bigg
    Aug 28, 2009 at 5:13

3 Answers 3

9

I just had this as well so I think its worth having the correct answer.

The 2.8.1 MySql gem is not utf-8 friendly, so it sometimes will return UTF strings and lie to Rails, telling it that they are ASCII when in fact they are UTF-8. This makes things explode.

So: you can either monkey patch or get a compatible MySql gem. See: http://gnuu.org/2009/11/06/ruby19-rails-mysql-utf8/

2

There appears to be an issue with ERB's encoding in Ruby 1.9. More details are in this Lighthouse ticket. A patch with a workaround has been included, perhaps it works for you?

The problem is erb code in ruby 1.9 distribution. When it compiles the template code it forces a 'ASCII-8bit' encoding, the problem is when the template code has multibyte characters the template code is returned in a 'ASCII-8bit' string and when this string is concat with a 'UTF8' string with multibyte character the exception is raised because the strings between this encodings are only compatible when both only have seven-bit characters.

2
  • 1
    turns out the mysql-gem was the culprit. it returned all strings as ASCII-8BIT even if they are UTF8 in the DB. Using github.com/hectoregm/mysql-ruby/tree/master fixed it
    – tliff
    Aug 28, 2009 at 12:36
  • Sorry molf, I downvoted this cause after experiencing this issue as well it was the mysql gems fault, I believe that bug is in a bit of limbo no one is really sure if its an issue. Dec 6, 2009 at 10:11
0

There seems to be an incompatibility between Ruby 1.9x and the mysql gem with regard to how strings are passed back and forth (specifically the encoding of the strings).

To fix, run

gem install mysql2 

on the server and update the database configuration file to use this gem instead of the previous one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.