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

筅森魡賤 提交于 2019-11-29 02:21:37

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.

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!