Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are all strings ASCII-8BIT after I upgraded to Rails 3?

I upgraded to RoR 3.0.1 and Ruby to 1.9.2. Now all the strings in my views are ASCII-8BIT?

I believe I have my app set up to use UTF 8

application.rb

config.encoding = "utf-8"

database.yml

development:
  adapter: mysql
  encoding: utf8

I'm running

OS X
RVM rvm 1.0.16 
Ruby ruby-1.9.2-p0
Rails 3.0.1

I'd expect that the enoding would be UTF 8 not ASCII

business.desc.encoding
# ASCII-8BIT

Since 1.9.x can concatenate strings of different encodings we see a lot of errors like this.

<p class="description"><%= truncate(business.desc, :length => 17) %></p>

The Error

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

activesupport (3.0.1) lib/active_support/core_ext/string/output_safety.rb:74:in `concat'
activesupport (3.0.1) lib/active_support/core_ext/string/output_safety.rb:74:in `concat'
actionpack (3.0.1) lib/action_view/template/handlers/erb.rb:14:in `<<'
app/views/browse/businesses.html.erb:15:in `block in _app_views_browse_businesses_html_erb___1616718260917998189_2173630500__1134905695726531092'
app/views/browse/businesses.html.erb:3:in `each'
app/views/browse/businesses.html.erb:3:in `each_with_index'
app/views/browse/businesses.html.erb:3:in `_app_views_browse_businesses_html_erb___1616718260917998189_2173630500__1134905695726531092'

Does anyone else have this problem? Is ruby-1.9.2-p0 the correct version to use?

Thanks!

like image 491
jspooner Avatar asked Oct 22 '10 17:10

jspooner


3 Answers

horrible issue. You need to put this at the top of each file

# coding: UTF-8

UPDATE Use the magic_encoding as described be Nerian.

Does essentially same as the below, but better.

/UPDATE

I have a rake task I don't remember where I found (kudos to that guy!) which I have slightly modified, to have this on top of each file. I've heard people say the above(what you've done) should be sufficient, but it doesn't work for me...

Anyhow, this is the rake task, just copy paste it


lib/tasks/utf8encode.rake

# coding: UTF-8

desc "Manage the encoding header of Ruby files"
task :utf8_encode_headers => :environment do
  files = Array.new
  ["*.rb", "*.rake"].each do |extension|
    files.concat(Dir[ File.join(Dir.getwd.split(/\\/), "**", extension) ])
  end

  files.each do |file|
    content = File.read(file)
    next if content[0..16] == "# coding: UTF-8\n\n" ||
            content[0..22] == "# -*- coding: utf-8 -*-"

   ["\n\n", "\n"].each do |file_end|
      content = content.gsub(/(# encoding: UTF-8#{file_end})|(# coding: UTF-8#{file_end})|(# -*- coding: UTF-8 -*-#{file_end})|(# -*- coding: utf-8 -*-#{file_end})/i, "")
    end

    new_file = File.open(file, "w")
    new_file.write("# coding: UTF-8\n\n"+content)
    new_file.close
  end
end
like image 111
oma Avatar answered Oct 26 '22 06:10

oma


You need to add this to every .rb file:

<% # coding: UTF-8 %>

I use the gem magic_encoding for that.

$ cd app/ 
$ magic_encoding

The default is UTF-8, but you can specify whatever you want as an argument.

like image 21
Nerian Avatar answered Oct 26 '22 04:10

Nerian


I'm moving from Ruby 1.8.6 and Rails 2.3.5 to Ruby 1.9.2 and Rails 3.0.3, with postregsql. In order to get this working on my project, I had to do add this to the top of any of my view templates that were being translated:

<% # coding: UTF-8 %>

The rake task provided by Ole should be easy to modify to do this as well. I didn't find his solution as given had any effect, though.

like image 28
Bob Wentz Avatar answered Oct 26 '22 05:10

Bob Wentz