“\xC2” to UTF-8 in conversion from ASCII-8BIT to UTF-8

本小妞迷上赌 提交于 2019-12-12 12:09:24

问题


I have a rails project that runs fine with MRI 1.9.3. When I try to run with Rubinius I get this error in app/views/layouts/application.html.haml:

"\xC2" to UTF-8 in conversion from ASCII-8BIT to UTF-8


回答1:


It turns out the page had an invalid character (an interpunct '·'), which I found out with the following code (credits to this gist and this question):

lines = IO.readlines("app/views/layouts/application.html.haml").map do |line|
  line.force_encoding('ASCII-8BIT').encode('UTF-8', :invalid => :replace, :undef => :replace, :replace => '?')
end

File.open("app/views/layouts/application.html.haml", "w") do |file|
  file.puts(lines)
end

After running this code, I could find the problematic characters with a simple git diff and moved the code to a helper file with # encoding: utf-8 at the top. I'm not sure why this doesn't fail with MRI but it should since I'm not specifying the encoding of the haml file.



来源:https://stackoverflow.com/questions/16218838/xc2-to-utf-8-in-conversion-from-ascii-8bit-to-utf-8

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