为什么JRuby给我一个“不兼容的字符编码:ASCII-8BIT和UTF-8”错误?

时间:2012-09-03 00:28:57

标签: ruby-on-rails encoding jruby

我有一个Rails 3.2.8应用程序。它在1.9兼容模式下在Ruby 1.9.3和JRuby 1.6.7.2下运行。插入数据库有时会在JRuby下崩溃。

这是我的en.yml文件:

en:
  moo: "you’re"
  meep: "you"
  bar: "\"Booón\""
  baz: "Booón"

假设我有一个Message对象,它有两个属性,sender和subject。以下是发件人和主题的几种可能组合

msg = Message.new(subject: "sblah", sender: I18n.t("bar"))
msg.save

msg = Message.new(subject: "sblah", sender: I18n.t("baz"))
msg.save

msg = Message.new(subject: I18n.t("moo"), sender: I18n.t("baz"))
msg.save

msg = Message.new(subject: I18n.t("moo"), sender: "temp")
msg.save

msg = Message.new(subject: I18n.t("meep"), sender: I18n.t("bar"))
msg.save

# Mystery crash
msg = Message.new(subject: I18n.t("moo"), sender: I18n.t("bar"))
msg.save

在JRuby 1.6.7.2(1.9兼容性)下运行时,最后一个导致崩溃。 MRI 1.9.3p194崩溃不会发生

似乎需要(重音字符和引号)和(卷曲撇号)的组合来触发崩溃。把这些东西拿掉。没有崩溃。

Encoding::CompatibilityError: incompatible character encodings: ASCII-8BIT and UTF-8
    from org/jruby/RubyString.java:2926:in `gsub'
    from /Users/paul/.rbenv/versions/jruby-1.6.7.2/lib/ruby/gems/1.8/gems/activerecord-jdbc-adapter-1.2.2/lib/arjdbc/mysql/adapter.rb:485:in `exec_insert'
    from /Users/paul/.rbenv/versions/jruby-1.6.7.2/lib/ruby/gems/1.8/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/database_statements.rb:90:in `insert'
    from /Users/paul/.rbenv/versions/jruby-1.6.7.2/lib/ruby/gems/1.8/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/query_cache.rb:19:in `insert'
    from /Users/paul/.rbenv/versions/jruby-1.6.7.2/lib/ruby/gems/1.8/gems/activerecord-3.2.8/lib/active_record/relation.rb:66:in `insert'
    from /Users/paul/.rbenv/versions/jruby-1.6.7.2/lib/ruby/gems/1.8/gems/activerecord-3.2.8/lib/active_record/persistence.rb:366:in `create'
    from /Users/paul/.rbenv/versions/jruby-1.6.7.2/lib/ruby/gems/1.8/gems/activerecord-3.2.8/lib/active_record/timestamp.rb:57:in `create'
    from /Users/paul/.rbenv/versions/jruby-1.6.7.2/lib/ruby/gems/1.8/gems/activerecord-3.2.8/lib/active_record/callbacks.rb:268:in `create'
    from /Users/paul/.rbenv/versions/jruby-1.6.7.2/lib/ruby/gems/1.8/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:406:in `_run__142677804__create__486269792__callbacks'
    from org/jruby/RubyBasicObject.java:1698:in `__send__'
    from org/jruby/RubyKernel.java:2097:in `send'
    from /Users/paul/.rbenv/versions/jruby-1.6.7.2/lib/ruby/gems/1.8/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:405:in `__run_callback'
    from /Users/paul/.rbenv/versions/jruby-1.6.7.2/lib/ruby/gems/1.8/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:390:in `_run_create_callbacks'
    from org/jruby/RubyBasicObject.java:1698:in `__send__'
    from org/jruby/RubyKernel.java:2097:in `send'
    from /Users/paul/.rbenv/versions/jruby-1.6.7.2/lib/ruby/gems/1.8/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:81:in `run_callbacks'
... 17 levels...

这是崩溃的代码:

480       def exec_insert(sql, name, binds)
481         binds = binds.dup
482 
483         # Pretend to support bind parameters
484         unless binds.empty?
485           sql = sql.gsub('?') { quote(*binds.shift.reverse) }                                       
486         end
487         execute sql, name
488       end

0 个答案:

没有答案