我在从 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 ����������������������������������������������������� .....
所以很明显我有一些无效的 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))
有什么想法、想法或建议吗?谢谢
最佳答案
当在 Heroku 上遇到这个问题时,我们转换为 US-ASCII 以适本地清理传入数据(即从 Word 粘贴):
Iconv.conv("UTF-8//IGNORE", "US-ASCII", content)
有了这个,我们就没有更多的字符编码问题了。
另外,仔细检查是否没有其他字段需要相同的转换,因为它可能会影响将文本 block 传递到数据库的任何内容。
关于ruby-on-rails - PGError : ERROR: invalid byte sequence for encoding "UTF8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4771985/