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

我刚刚开始从Ruby 1.8.7升级到Ruby 1.9.2(使用RVM)。 我的所有应用程序都使用’script / server’(或’rails server’)和1.9.2运行,但只有Rails 3.0.0 RC应用程序可以与Passenger一起使用。 Rails 2.3.8应用程序给出的错误消息是:

US-ASCII中的无效字节序列

我猜这是一个乘客问题。 我使用此处的RVM指南安装了Passenger 2.2.15。 任何想法如何解决这个错误? 谢谢。 我已更新为包含堆栈跟踪:

/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文件的顶部。 如果可行,则在源文件中处理非US ASCII字符。

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

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

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

我在Ubuntu(11.10)上有类似的问题,因为这是在我的/ etc / apache2 / envvars中:

 ## The locale used by some modules like mod_dav export LANG=C ## Uncomment the following line to use the system default locale instead: #. /etc/default/locale 

将注释交换到这一个以使用/ etc / default / locale(包含LANG="en_US.UTF-8" )解决了我的问题,而无需为我的ruby创建一个包装器。

我有一个类似的问题与不同的服务器, 环境变量结果是罪魁祸首 。

我同意pjmorse关于环境变量的原因,特别是在我的Passenger / Rails设置中,罪魁祸首是LANG值。

通过脚本/服务器启动我的Rails应用程序时,我有LANG = en_CA.UTF-8,但在使用Passenger提升应用程序时没有。

解决方案:修改Passenger配置以使用包装器启动Ruby,请参阅http://blog.phusion.nl/2008/12/16/passing-environment-variables-to-ruby-from-phusion-passenger/

使用它作为包装器:

 #!/bin/sh export LANG=en_CA.UTF-8 exec "/Your_Default_Ruby_Path/ruby" "$@" 

注意在设置包装器之前,Your_Default_Ruby_Path是http.conf的PassengerRuby值中的任何值。

欢迎来到强制字符串编码的精彩世界; 错误是Ruby 1.9.x vs Ruby 1.8.x字符串中的行为差异。

查看http://blog.phusion.nl/2009/02/02/getting-ready-for-ruby-191/ – 可能会有所帮助。 您可能只需要更新您的gemset。