gpt4 book ai didi

ruby-on-rails - Rails 3,heroku - PGError : ERROR: invalid byte sequence for encoding "UTF8":

转载 作者:行者123 更新时间:2023-12-03 00:41:18 34 4
gpt4 key购买 nike

我只是通过 Rails 3 在 Heroku (postgres) 上随机得到这个奇怪的错误

PGError: ERROR: invalid byte sequence for encoding "UTF8": 0x85 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 ('BTW∑I re-listened to the video' ......

这个提示虽然不错,但并没有让我有任何点击。我可以在某处设置编码吗?我应该搞乱它吗?有人看到过这个和/或对如何处理此类问题有任何想法吗?

谢谢

最佳答案

据我所知,这是一个问题,您尝试插入 PostgrSQL 服务器的字符串未使用 UTF-8 编码。这有点奇怪,因为您的 Rails 应用程序应该配置为默认使用 UTF-8。

您可以尝试通过多种方法解决此问题(按照我推荐的顺序):

  • 首先,确保 config/application.rb 中的 config.encoding 设置为 "utf-8"

  • 如果您使用的是 Ruby 1.9,您可以尝试在插入之前使用 toutf8 强制进行字符编码。

  • 您可以找出字符串的编码方式,然后在 PostgeSQL 连接上手动设置 SET CLIENT_ENCODING TO 'ISO-8859-1'; (或任何编码)插入字符串。不要忘记在重置编码的语句后执行 RESET CLIENT_ENCODING;

  • 如果您使用的是 Ruby 1.8(可能性更大),则可以使用 iconv 库将字符串转换为 UTF-8。请参阅文档 here .

  • 一个更黑客的解决方案是覆盖模型中的 getter 和 setter(即 contentcontent=),使用 Base64 对字符串进行编码和解码。它看起来像这样:

 

require 'base64'

class Comment
def content
Base64::decode64(self[:content])
end

def content=(value)
self[:content] = Base64::encode64(value)
end
end

关于ruby-on-rails - Rails 3,heroku - PGError : ERROR: invalid byte sequence for encoding "UTF8":,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4741404/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com