Rails 3 - 如何处理PG错误不完整的多字节字符 [英] Rails 3 - How to handle PG Error incomplete multibyte character

查看:152
本文介绍了Rails 3 - 如何处理PG错误不完整的多字节字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Rails 3.2应用程序(Ruby 1.9.2)中,我收到以下错误

In a Rails 3.2 app (Ruby 1.9.2) I am getting the following errors


在mobile_users#update中发生PGError:

A PGError occurred in mobile_users#update:

不完整的多字节字符

这些是Postgres错误我得到类似的SQLIte在开发和测试模式下测试时出现错误

These are Postgres errors bu I get similar SQLIte error when testing in dev and test modes

导致此错误的参数(auth令人意图省略)

The params that cause this error are (auth token deliberately omitted)

  * Parameters: {"mobile_user"=>{"quiz_id"=>"1", "auth"=>"xxx", "name"=>"Joaqu\xEDn"}, "action"=>"update", "controller"=>"mobile_users", "id"=>"1", "format"=>"mobile"}

这是一个JSON HTTP Put请求,更新操作如下:

This is coming in as a JSON HTTP Put request and the update action dealing with this is as follows

  # PUT /mobile_users/1
  # PUT /mobile_users/1.xml
  def update
    @mobile_user = current_mobile_user
    @mobile_user.attributes = params[:mobile_user]

    respond_to do |format|
      if @mobile_user.save
        format.html { redirect_to(@mobile_user, :notice => 'Mobile user was successfully updated.') }
        format.json  { head :ok }
        format.mobile  { head :ok }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.json  { render :json => @mobile_user.errors, :status => :unprocessable_entity }
        format.mobile  { render :json => @mobile_user.errors, :status => :unprocessable_entity }
        format.xml  { render :xml => @mobile_user.errors, :status => :unprocessable_entity }
      end
    end

  end

在上述参数中的违规字符串是完全有效的Joaqu\xEDn。
的事情是,我需要处理任何语言的所有字符集。

The offending string is in the above params is "Joaqu\xEDn" which is perfectly valid. the thing is that I need to handle all character sets from any language.

我假设我需要使用iconv库,但为了做到这一点我需要检测到转换为UTF8的字符集,我不知道如何做到这一点。

I assume I would need to use the iconv library but in order to do that I would need to detect the character set to convert to UTF8 from and I haven't a clue how to do this.

我也在UTF- 8为name=>p \xEDa

I am also getting invalid byte sequence in UTF-8 for "name"=>"p\xEDa "

推荐答案

p> This:

"Joaqu\xEDn"

是ISO-8859-1编码版本的Joaquín,所以它是无效的UTF-8和你的数据库是正确的抱怨。如果可能,请修复您的移动客户端在JSON中使用UTF-8;如果您不能这样做,那么您可以使用以下方式修复编码:

is the ISO-8859-1 encoded version of "Joaquín" so it is not valid UTF-8 and your databases are right to complain about it. If possible, fix your mobile clients to use UTF-8 in the JSON; if you can't do that then you can fix the encoding with this:

params[:mobile_user][:name].force_encoding('iso-8859-1').encode!('utf-8')

服务器。在服务器上修复它的问题是您必须猜测输入的编码是什么,您的猜测可能不正确。没有办法可靠地猜测特定字符串的编码,有 rchardet ,但它不起作用最近版本的Ruby,似乎已被放弃;您可能可以修复这个宝石来使用现代Ruby。还有一些其他猜测图书馆,但他们似乎都被放弃了。

on the server. The problem with fixing it on the server is that you have to guess what the incoming encoding is and your guess might not be correct. There is no way to reliably guess the encoding for a particular string, there is rchardet but it doesn't work with recent versions of Ruby and it appears to have been abandoned; you might be able to fix this gem to work with modern Ruby. There are a few other guessing libraries but they all seem to be have been abandoned as well.

JSON文字总是,根据定义,Unicode和UTF-8默认编码:

JSON text is always, by definition, Unicode and UTF-8 encoded by default:

3.  Encoding

   JSON text SHALL be encoded in Unicode.  The default encoding is
   UTF-8.

发送给您的不是UTF-8的JSON的任何客户端都是IMO,因为几乎所有的东西将假定JSON将是UTF-8。当然,可能有一个编码标题在某个地方指定ISO 8859-1,或者标题说UTF-8即使是ISO 8859-1。

Any clients that are sending you JSON that isn't in UTF-8 is IMO broken because almost everything will assume that JSON will be UTF-8. Of course, there might be an encoding header somewhere that specifies ISO 8859-1 or maybe the headers say UTF-8 even though it is ISO 8859-1.

这篇关于Rails 3 - 如何处理PG错误不完整的多字节字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆