Wednesday, June 22, 2011

incompatible character encodings: ASCII-8BIT and UTF-8 in Ruby 1.9.2

While migrating from Ruby 1.8.7 to Ruby 1.9.2 getting error:
incompatible character encodings: ASCII-8BIT and UTF-8 in Ruby 1.9.2

While running rake task, getting error:

rake aborted!
C:/projects/restaurant/lib/tasks/setup.rake:158: invalid multibyte char (US-ASCII)
C:/projects/restaurant/lib/tasks/setup.rake:158: invalid multibyte char (US-ASCII)
C:/projects/restaurant/lib/tasks/setup.rake:158: syntax error, unexpected $end, expecting ')'
     Cuisine.create(:name=>"Caf⌐")
                                 ^
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:235:in `load'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:235:in `block in load'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:225:in `block in load_dependency'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:596:in `new_constants_in'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:225:in `load_dependency'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:235:in `load'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.5/lib/rails/engine.rb:131:in `block in load_tasks'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.5/lib/rails/engine.rb:131:in `each'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.5/lib/rails/engine.rb:131:in `load_tasks'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.5/lib/rails/application.rb:141:in `load_tasks'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.5/lib/rails/application.rb:77:in `method_missing'
C:/projects/restaurant/Rakefile:7:in `<top (required)>'
C:/Ruby192/lib/ruby/1.9.1/rake.rb:2373:in `load'
C:/Ruby192/lib/ruby/1.9.1/rake.rb:2373:in `raw_load_rakefile'
C:/Ruby192/lib/ruby/1.9.1/rake.rb:2007:in `block in load_rakefile'
C:/Ruby192/lib/ruby/1.9.1/rake.rb:2058:in `standard_exception_handling'
C:/Ruby192/lib/ruby/1.9.1/rake.rb:2006:in `load_rakefile'
C:/Ruby192/lib/ruby/1.9.1/rake.rb:1991:in `run'
C:/Ruby192/bin/rake:31:in `<main>'

At some other places getting error :
invalid multibyte char (US-ASCII)


Solution:
Solved it by following steps:
  • Make sure 'config.encoding = "utf-8"' is there in application.rb file.
  • Make sure you are using 'mysql2' gem
  • Putting '# encoding: utf-8' on top of rake file.
  • Above 'Starter::Application.initialize!' line in environment.rb file, add following two lines:
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8

2 comments:

  1. mysql was the problem in my case. mysql2 solved it. thanks!

    ReplyDelete
  2. Ran into the same problem and the problem was indeed the `mysql` gem that was replaced with the `mysql2` gem. Also Digest::MD5.digest caused me some grief. Instead you should use: hexdigest. To learn how to convert between the two: http://stackoverflow.com/questions/22700113/convert-between-digestmd5-digest-and-digestmd5-hexdigest/22700114

    ReplyDelete