升级到Rails 3后,为什么所有字符串都是ASCII-8BIT?
我升级到RoR 3.0.1和Ruby升级到1.9.2。 现在我视图中的所有字符串都是ASCII-8BIT?
我相信我的应用程序设置为使用UTF 8
application.rb中
config.encoding = "utf-8"
database.yml的
development: adapter: mysql encoding: utf8
我在跑
OS X RVM rvm 1.0.16 Ruby ruby-1.9.2-p0 Rails 3.0.1
我希望编码是UTF 8而不是ASCII
business.desc.encoding # ASCII-8BIT
由于1.9.x可以连接不同编码的字符串,我们会看到很多这样的错误。
17) %>
错误
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'
还有其他人有这个问题吗? ruby-1.9.2-p0是否使用正确的版本?
谢谢!
您需要将其添加到每个.rb文件:
<% # coding: UTF-8 %>
我使用gem magic_encoding。
$ cd app/ $ magic_encoding
默认值为UTF-8,但您可以指定任何您想要的参数。
可怕的问题。 您需要将它放在每个文件的顶部
# coding: UTF-8
更新使用如Nerian所述的magic_encoding。
与下面的内容基本相同,但更好。
/ UPDATE
我有一个rake任务,我不记得我找到了什么(对那个人的称赞!)我稍微修改了一下,把它放在每个文件的顶部。 我听说有人说上面(你做过的)应该足够了,但它对我不起作用……
无论如何,这是rake任务,只需复制粘贴即可
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
我正在使用postregsql从Ruby 1.8.6和Rails 2.3.5迁移到Ruby 1.9.2和Rails 3.0.3。 为了使这个工作在我的项目上,我不得不将其添加到我正在翻译的任何视图模板的顶部:
<% # coding: UTF-8 %>
Ole提供的rake任务也应该易于修改以完成此操作。 但是,我没有找到他给出的解决方案有任何影响。