1

我正在开发一个从 rails 3.2 和 ruby​​ 1.9.x 迁移到 ruby​​ 2.2.x 和 rails 4.2.4 版本的项目。

当项目进入生产服务器时,它开始抛出如下编码错误:

"\xC3" from ASCII-8BIT to UTF-8

后跟一个堆栈跟踪,如:

["/var/www/suba/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.4/lib/active_support/core_ext/object/json.rb:34:in `encode'"
 "/var/www/suba/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.4/lib/active_support/core_ext/object/json.rb:34:in `to_json'"
 "/var/www/suba/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.4/lib/active_support/core_ext/object/json.rb:34:in `to_json_with_active_support_encoder'"
 "/var/www/suba/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.4/lib/active_support/json/encoding.rb:57:in `to_json'"
 "/var/www/suba/vendor/bundle/ruby/2.2.0/gems/json-1.8.3/lib/json/common.rb:223:in `generate'"
 "/var/www/suba/vendor/bundle/ruby/2.2.0/gems/json-1.8.3/lib/json/common.rb:223:in `generate'"
 "/var/www/suba/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.4/lib/active_support/json/encoding.rb:101:in `stringify'"
 "/var/www/suba/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.4/lib/active_support/json/encoding.rb:35:in `encode'"
 "/var/www/suba/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.4/lib/active_support/json/encoding.rb:22:in `encode'"
 "/var/www/suba/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.4/lib/active_support/core_ext/object/json.rb:37:in `to_json_with_active_support_encoder'"
 "/var/www/suba/vendor/bundle/ruby/2.2.0/gems/multi_json-1.11.2/lib/multi_json/adapters/json_common.rb:19:in `dump'"
 "/var/www/suba/vendor/bundle/ruby/2.2.0/gems/multi_json-1.11.2/lib/multi_json/adapter.rb:25:in `dump'"
 "/var/www/suba/vendor/bundle/ruby/2.2.0/gems/multi_json-1.11.2/lib/multi_json.rb:136:in `dump'"
 "/var/www/suba/vendor/bundle/ruby/2.2.0/gems/resque-1.24.1/lib/resque/helpers.rb:25:in `encode'"
 "/var/www/suba/vendor/bundle/ruby/2.2.0/gems/resque-1.24.1/lib/resque.rb:173:in `push'"
 "/var/www/suba/vendor/bundle/ruby/2.2.0/gems/resque-1.24.1/lib/resque/job.rb:51:in `create'"
 "/var/www/suba/vendor/bundle/ruby/2.2.0/gems/resque-1.24.1/lib/resque.rb:271:in `enqueue_to'"
 "/var/www/suba/vendor/bundle/ruby/2.2.0/gems/resque-1.24.1/lib/resque.rb:252:in `enqueue'"
 "/var/www/suba/app/controllers/call_center/chat_controller.rb:6:in `transcript'"

和第chat_controller.rb6行transcript方法:

  def transcript
    Resque.enqueue(Chat::SessionTranscript, read_request_body)
    head :ok
  end

  def read_request_body
    xml = request.body.read
    Rails.logger.info("XML: #{xml}")
    xml
  end

在我们代码库的另一点中,我们使用如下代码解决了这个问题:

string.encode("UTF-8", :invalid => :replace, :undef => :replace, :replace => "?")

但同样的错误开始出现在其他部分。

有没有人通过这个,你是如何处理的?

提前谢谢你。

4

1 回答 1

0

我通过替换像 34 at 解决了这个问题

(...)/activesupport-4.2.4/lib/active_support/core_ext/object/json.rb

为了:

self.force_encoding('utf-8').to_json_without_active_support_encoder(options)
于 2016-10-24T23:13:11.003 回答