问题描述
ActiveRecord :: StatementInvalid:PG ::错误:错误:编码UTF8的无效字节序列:0xfc
我想这个字符串正在发生Mühldorf
。
我尝试添加 #encoding:utf-8
到我的ruby文件的顶部来解决这个问题,但似乎没有做任何事情。
我使用来提取位置数据,这是字符串来自哪里。
我在Heroku上的Postgres数据库上运行Ruby 1.9.3。
案例U-umlaut在(AKA Latin-1)中为0xfc,但0xfc不是有效的UTF-8字符。问题是您有一个Latin-1字符串,您正在尝试将其视为UTF-8,PostgreSQL正确地抱怨。
修复要发送的数据源你UTF-8或者如果它总是发给你拉丁字母1,你可以用以下方式修改编码:
utf_8_string = latin_1_string.force_encoding('iso8859-1')。encode('utf-8')
然后使用 utf_8_string
版本。
I'm getting the following ActiveRecord error when certain strings are saved to the database.
ActiveRecord::StatementInvalid: PG::Error: ERROR: invalid byte sequence for encoding "UTF8": 0xfc
I think it's happening for this string Mühldorf
.
I've tried adding # encoding: utf-8
to the top of my ruby files to solve this, but doesn't seem to be doing anything.
I'm pulling location data using Ruby Geocoder, and that's where the string is coming from.
I'm running Ruby 1.9.3 on Postgres database on Heroku.
A lower case U-umlaut is 0xfc in ISO 8859-1 (AKA Latin-1) but 0xfc is not a valid UTF-8 character. The problem is that you have a Latin-1 string that you're trying to treat as UTF-8 and PostgreSQL is rightly complaining.
Either fix the data source to send you UTF-8 or, if it will always send you Latin-1, fix the encoding yourself with something like:
utf_8_string = latin_1_string.force_encoding('iso8859-1').encode('utf-8')
and then work with the utf_8_string
version.
这篇关于PG ::错误:错误:无效字节序列用于编码“UTF8”:0xfc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!