5

我刚刚开始从 Ruby 1.8.7 升级到 Ruby 1.9.2(使用 RVM)。我的所有应用程序都使用 1.9.2 的“脚本/服务器”(或“rails 服务器”)运行,但是,只有 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'
4

5 回答 5

6

尝试添加

# 编码:UTF-8
在 main_controller.rb 文件的顶部。如果可行,则您正在处理源文件中的非美国 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关于编码的精彩文章。

于 2010-08-19T00:41:11.170 回答
5

我在 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​​ 制作包装器。

于 2012-03-03T18:56:44.237 回答
3

我在另一台服务器上遇到了类似的问题,结果是环境变量是罪魁祸首

于 2010-09-08T15:57:14.740 回答
2

我同意 pjmorse 关于环境变量的原因,特别是在我的乘客/铁路设置中,罪魁祸首是 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 值中的任何内容。

于 2010-12-01T20:08:57.720 回答
1

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

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

于 2010-08-18T20:33:31.630 回答