RVM、Ruby 1.9.2、Rails 2.3.8、Passenger 和“US-ASCII 中的无效字节序列"

问题描述:

我刚刚开始从 Ruby 1.8.7 升级到 Ruby 1.9.2(使用 RVM).我的所有应用程序都使用带有 1.9.2 的脚本/服务器"(或rails 服务器")运行,但是,只有 Rails 3.0.0 RC 应用程序可以与乘客一起使用.Rails 2.3.8 应用程序给出的错误信息是:

I just started the upgrade process from Ruby 1.8.7 to Ruby 1.9.2 (using RVM). I have all my applications running using 'script/server' (or 'rails server') with 1.9.2, however, only Rails 3.0.0 RC applications work with Passenger. The error message given by Rails 2.3.8 applications is:

US-ASCII 中的无效字节序列

invalid byte sequence in US-ASCII

我猜这是一个乘客问题.我使用 here 的 RVM 指南安装了Passenger 2.2.15.任何想法如何修复这个错误?谢谢.我已更新以包含堆栈跟踪:

I'm guessing that this is a Passenger issue. I installed Passenger 2.2.15 using the RVM guide found here. Any ideas how to fix this bug? Thanks. I've updated to include a stack trace:

/Users/kevin/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-2.3.8/lib/action_view/template_handlers/erb.rb:14:in `compile'
/Users/kevin/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-2.3.8/lib/action_view/template_handler.rb:11:in `call'
/Users/kevin/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-2.3.8/lib/action_view/renderable.rb:19:in `compiled_source'
/Users/kevin/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-2.3.8/lib/action_view/renderable.rb:68:in `compile!'
/Users/kevin/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-2.3.8/lib/action_view/renderable.rb:61:in `compile'
/Users/kevin/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-2.3.8/lib/action_view/renderable.rb:28:in `render'
/Users/kevin/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-2.3.8/lib/action_view/template.rb:205:in `render_template'
/Users/kevin/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-2.3.8/lib/action_view/base.rb:265:in `render'
/Users/kevin/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-2.3.8/lib/action_view/base.rb:352:in `_render_with_layout'
/Users/kevin/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-2.3.8/lib/action_view/base.rb:262:in `render'
/Users/kevin/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-2.3.8/lib/action_controller/base.rb:1250:in `render_for_file'
/Users/kevin/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-2.3.8/lib/action_controller/base.rb:942:in `render'
/Users/kevin/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-2.3.8/lib/action_controller/benchmarking.rb:51:in `block in render_with_benchmark'
/Users/kevin/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-2.3.8/lib/active_support/core_ext/benchmark.rb:17:in `block in ms'
/Users/kevin/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/benchmark.rb:309:in `realtime'
/Users/kevin/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-2.3.8/lib/active_support/core_ext/benchmark.rb:17:in `ms'
/Users/kevin/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-2.3.8/lib/action_controller/benchmarking.rb:51:in `render_with_benchmark'
/Users/kevin/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-2.3.8/lib/action_controller/mime_responds.rb:135:in `block in custom'
/Users/kevin/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-2.3.8/lib/action_controller/mime_responds.rb:179:in `call'
/Users/kevin/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-2.3.8/lib/action_controller/mime_responds.rb:179:in `block in respond'
/Users/kevin/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-2.3.8/lib/action_controller/mime_responds.rb:173:in `each'
/Users/kevin/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-2.3.8/lib/action_controller/mime_responds.rb:173:in `respond'
/Users/kevin/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-2.3.8/lib/action_controller/mime_responds.rb:107:in `respond_to'
/Users/kevin/Sites/sample/app/controllers/main_controller.rb:7:in `index'

尝试添加

# encoding: UTF-8

在 main_controller.rb 文件的顶部.如果可行,则说明您正在处理源文件中的非美国 ASCII 字符.

at the top of your main_controller.rb file. If that works, you're dealing with a non US ASCII character in your source file.

在 Ruby 1.9 中,我们处理三种编码上下文:

In Ruby 1.9, we're dealing with three encoding contexts:

  • 源代码编码:默认情况下,源文件中的字符串被解释为 US-ASCII,除非我在上面列出的魔术注释存在.
  • 外部编码:假定文本文件中的字符与环境具有相同的编码.但是,可以指定要使用的编码.例如:open(mydata.txt, "r:UTF-8").
  • 内部编码:指定从文件中读取文本数据后如何编码.默认情况下这是 nil,这意味着它将与用于读取它的编码相同.如果需要不同的东西,可以在 IO.open 中指定.例如: open(mydata.txt, 'r:UTF-8:UTF-16LE')

有关更多信息,我会阅读James Edward Gray II 撰写的关于编码的精彩文章.

For more info, I'd read James Edward Gray II's great articles on encoding.