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

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?

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

<%# .. %> is a comment, perhaps you meant <%-

Colin

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

No, all I found on google said I have to put it as a comment, <%- gives
a syntax error.

I just run into the same problem in two other projects. Is there a way
to set the encoding application wide?

No, all I found on google said I have to put it as a comment, <%- gives
a syntax error.

You seem to have snipped the useful stuff. How about encoding rather
than coding? Though I am not convinced.

Colin

Sorry, that was a typo. Even though it's still not working.

In every of my 4 projects with this error it always throws it when a
special char is being displayed like ä, ö , ü, é, à etc.

It would be cool if I just need to paste some "# encoding: utf-8" or
something somewhere and it recognizes everything as utf-8, is there
anything like that I could do?

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

Hey,

First of all thanks but I tried what you said and well...
gem install ruby-mysql
config/environment.rb: added config.gem 'ruby-mysql'
config/database.yml: added adapter: mysql, encoding: utf8

But when I start the server I get following error:
=> Booting WEBrick
=> Rails 2.3.5 application starting on http://0.0.0.0:3000
D:/ruby19/lib/ruby/gems/1.9.1/gems/rails-2.3.5/lib/rails/gem_dependency.rb:119:Warning:
Gem::Dependency#version_requirements is deprecated and will be removed
on or after August 2010. Use #requirement
no such file to load -- ruby-mysql
...
D:/ruby19/lib/ruby/gems/1.9.1/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:156:in
`require'Missing these required gems:
  ruby-mysql

You're running:
  ruby 1.9.1.378 at D:/ruby19/bin/ruby.exe
  rubygems at D:/ruby19/lib/ruby/gems/1.9.1

Run `rake gems:install` to install the missing gems.

I of course tried rake gems:install but it didn't help. I also tried to
just uninstall the mysql gem but that didn't work either. Any idea?

Any one knows what's wrong?

I'm ran into the same problems after switching to Ruby 1.9.1. Have you
found a solution yet?

Not yet, had to switch back to Ruby 1.8.6.

Maybe someone else has an idea?

If you have have hard coded strings in ruby code you should try
Mystring ="bla bla"
Mystring.force_encoding('ASCII-8BIT')
Before using it.
For database strings you should search for mysql to force encoding
(this is for db created with ruby 1.8)
Good luck
Heinz Strunk wrote:

That means if I do a "db:migrate:reset" (recreate the db with Ruby 1.9)
that error would disappear?

I tried it, no it doesn't :frowning:

FYI: I execute these commands:
db:drop
db:create
db:setup

Still same error.

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

Most likely, you are outputting content stored in your DB as UTF-8 onto
a template that is ASCII. This fix works for me:
http://gist.github.com/273741 -- drop this into
config/initializers/ruby_191_hacks.rb. I posted more about upgrading to
1.9.1 here:
http://earthcode.com/blog/2010/01/rails_on_ruby_191_in_productio.html

Are you sure you don't have hard coded strings in the partial or
strings assigned
to variables through controller?

Andre Lewis wrote:

Andre Lewis wrote:

Most likely, you are outputting content stored in your DB as UTF-8 onto
a template that is ASCII. This fix works for me:
http://gist.github.com/273741 -- drop this into
config/initializers/ruby_191_hacks.rb. I posted more about upgrading to
1.9.1 here:
http://earthcode.com/blog/2010/01/rails_on_ruby_191_in_productio.html

Unfortunately not working:
=> Booting WEBrick
=> Rails 2.3.5 application starting on http://0.0.0.0:3000
D:/ruby19/lib/ruby/gems/1.9.1/gems/rails-2.3.5/lib/rails/gem_dependency.rb:119:Warning:
Gem::Dependency#version_requirements is deprecated and will be removed
on or after August 2010. Use #requirement
D:/ruby19/lib/ruby/site_ruby/1.9.1/rubygems/version.rb:186:in `strip!':
can't modify frozen string (RuntimeError)
        from
D:/ruby19/lib/ruby/site_ruby/1.9.1/rubygems/version.rb:186:in
`initialize'
        from
F:/NetBeansProjects/myproject/config/initializers/ruby_191_hacks.rb:12:in
`new'
        from
F:/NetBeansProjects/myproject/config/initializers/ruby_191_hacks.rb:12:in
`<top (required)>'
        from
D:/ruby19/lib/ruby/gems/1.9.1/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:145:in
`load'
        from
D:/ruby19/lib/ruby/gems/1.9.1/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:145:in
`block in load_with_new_constant_marking'
        from
D:/ruby19/lib/ruby/gems/1.9.1/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:521:in
`new_constants_in'
        from
D:/ruby19/lib/ruby/gems/1.9.1/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:145:in
`load_with_new_constant_marking'
        from
D:/ruby19/lib/ruby/gems/1.9.1/gems/rails-2.3.5/lib/initializer.rb:622:in
`block in load_application_initializers'
        from
D:/ruby19/lib/ruby/gems/1.9.1/gems/rails-2.3.5/lib/initializer.rb:621:in
`each'
        from
D:/ruby19/lib/ruby/gems/1.9.1/gems/rails-2.3.5/lib/initializer.rb:621:in
`load_application_initializers'
        from
D:/ruby19/lib/ruby/gems/1.9.1/gems/rails-2.3.5/lib/initializer.rb:176:in
`process'
        from
D:/ruby19/lib/ruby/gems/1.9.1/gems/rails-2.3.5/lib/initializer.rb:113:in
`run'
        from F:/NetBeansProjects/myproject/config/environment.rb:10:in
`<top (required)>'
        from
D:/ruby19/lib/ruby/gems/1.9.1/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:156:in
`require'
        from
D:/ruby19/lib/ruby/gems/1.9.1/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:156:in
`block in require'
        from
D:/ruby19/lib/ruby/gems/1.9.1/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:521:in
`new_constants_in'
        from
D:/ruby19/lib/ruby/gems/1.9.1/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:156:in
`require'
        from
D:/ruby19/lib/ruby/gems/1.9.1/gems/rails-2.3.5/lib/commands/server.rb:84:in
`<top (required)>'
        from script/server:3:in `require'
        from script/server:3:in `<main>'

Manos wrote:

Are you sure you don't have hard coded strings in the partial or
strings assigned
to variables through controller?

I'm only using I18n.t for strings and globalize2 for model translation.

Oh what I forgot. The icompati... error is showing on a line where I'm
using I18n.t and it would return a string with a umlaut.