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

时间:2011-01-23 02:35:40

标签: ruby-on-rails postgresql ruby-on-rails-3 encoding heroku

我从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))

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

1 个答案:

答案 0 :(得分:8)

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

Iconv.conv("UTF-8//IGNORE", "US-ASCII", content)

有了这个,我们就不再遇到字符编码问题了。

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

相关问题