PGError:错误:编码"UTF8"的无效字节序列

AnA*_*ice 6 postgresql encoding ruby-on-rails heroku ruby-on-rails-3

我从Cloudmailin收集Rails电子邮件时收到以下PGError:

PGError: ERROR: invalid byte sequence for encoding "UTF8": 0xbb HINT: This error can also happen if the byte sequence does not match the encoding expected by the server, which is controlled by "client_encoding". : INSERT INTO "comments" ("content") VALUES ('Reply with blah blah  ????????????????????????????????????????????????????? .....
Run Code Online (Sandbox Code Playgroud)

所以很明显我有一些无效的UTF8字符进入电子邮件吧?所以我试着清理它,但仍有一些东西在偷偷摸摸.这是我到目前为止所拥有的:

message_all_clean = params[:message]
Iconv.conv('UTF-8//IGNORE', 'UTF-8', message_all_clean)
message_plain_clean = params[:plain]
Iconv.conv('UTF-8//IGNORE', 'UTF-8', message_plain_clean)

@incoming_mail = IncomingMail.create(:message_all => Base64.encode64(message_all_clean), :message_plain => Base64.encode64(message_plain_clean))
Run Code Online (Sandbox Code Playgroud)

任何想法,想法或建议?谢谢

Dom*_*nic 8

当在Heroku上遇到此问题时,我们转换为US-ASCII以适当地清理传入的数据(即从Word粘贴):

Iconv.conv("UTF-8//IGNORE", "US-ASCII", content)
Run Code Online (Sandbox Code Playgroud)

有了这个,我们没有更多的字符编码问题.

另外,请仔细检查是否存在其他需要相同转换的字段,因为它可能会影响将文本块传递到数据库的任何内容.

  • 啊,我不确定.在实际操作中,我创建了一个Iconv对象(ic = Iconv.new("UTF-8 // IGNORE","US-ASCII")),然后使用了iconv方法:content = ic.iconv(content).Iconv.conv()似乎是一个简写,但我更喜欢有一个可重用的对象. (2认同)
  • 如何将其转换为不推荐使用Iconv的Ruby 1.9.3? (2认同)