jdo*_*dog 14 encoding ruby-on-rails utf-8 mongodb
如果开发人员编写此方法并导致Encoding :: UndefinedConversionError(从ASCII-8BIT到UTF-8的"\ xE2"):错误.
此错误只是随机发生,因此进入的数据是原始数据库字段是导致问题的原因.但由于我对此没有任何控制权,我可以在下面的方法中修复这么糟糕的数据不会导致任何问题?
def scrub_string(input, line_break = ' ')
begin
input.an_address.delete("^\u{0000}-\u{007F}").gsub("\n", line_break)
rescue
input || ''
end
end
Run Code Online (Sandbox Code Playgroud)
这会有用吗?
input = input.encode('utf-8', :invalid => :replace, :undef => :replace, :replace => '_')
Run Code Online (Sandbox Code Playgroud)
是的,这应该可行,它将用下划线替换任何无法转换为 UTF-8 的奇怪字符。
阅读有关 ruby 中字符串编码的更多信息:
http://ruby-doc.org/core-1.9.3/String.html#method-i-encode
| 归档时间: |
|
| 查看次数: |
4486 次 |
| 最近记录: |