[英]Ruby 2 and Rails 4 encoding issue
我正在做一個從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'"
和transcript
方法上的chat_controller.rb
第6行:
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 => "?")
但是相同的錯誤開始在其他部分出現。
有沒有人通過這個,您如何處理?
在此先感謝您。
我通過替換為34來解決了這個問題
(...)/的ActiveSupport-4.2.4 / LIB / active_support / core_ext /對象/ json.rb
對於:
self.force_encoding('utf-8').to_json_without_active_support_encoder(options)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.