【问题标题】:RVM, Ruby 1.9.2, Rails 2.3.8, Passenger and "invalid byte sequence in US-ASCII"RVM、Ruby 1.9.2、Rails 2.3.8、Passenger 和“US-ASCII 中的无效字节序列”
【发布时间】:2011-03-31 18:10:39
【问题描述】:

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

US-ASCII 中的无效字节序列

我猜这是乘客问题。我使用找到的 RVM 指南 here 安装了 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'

【问题讨论】:

  • 关于错误的任何其他信息?它发生在哪里?可能是堆栈跟踪?

标签: ruby-on-rails ruby passenger rvm


【解决方案1】:

尝试添加

#编码: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's great articles 的编码。

【讨论】:

  • 谢谢,但我在源文件中没有任何非美国 ASCII 字符。我认为这一定是乘客问题(鉴于项目在 mongrel 下都运行良好)。
【解决方案2】:

我在 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

将 cmets 替换为使用 /etc/default/locale(包含 LANG="en_US.UTF-8")为我解决了这个问题,而无需为我的 ruby​​ 制作包装器。

【讨论】:

  • 这个答案在 ubuntu 12.04 上对我有用,在 ruby​​ 1.9.3 中运行 rails 3.2 应用程序。应用程序中使用的一些 gem 在 gemspecs 中有 UTF-8 编码的字符。
  • 看准了!谢谢你,减轻了很多痛苦。
【解决方案3】:

我在另一台服务器上遇到了类似的问题,environment variables turned out to be the culprit

【讨论】:

    【解决方案4】:

    我同意 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 值中的任何内容。

    【讨论】:

      【解决方案5】:

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

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

      【讨论】:

      • 那么这是乘客宝石的问题吗?使用内置的 Mongreal(脚本/服务器),应用程序似乎运行良好。它似乎也没有解释为什么 3.0.0 RC 应用程序正在工作。有什么想法吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-24
      • 2011-04-10
      相关资源
      最近更新 更多