2010-08-18 27 views
5

我剛剛開始從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-ASCIIRVM,Ruby 1.9.2,Rails 2.3.8,Passenger和「US-ASCII中的無效字節序列」

我猜,這是一個問題,乘客

無效字節序列。我使用RVM指南安裝了Passenger 2.2.15,發現here。任何想法如何解決這個錯誤?謝謝。我已更新以包含堆棧跟蹤:

/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' 
+0

有關錯誤的其他信息?它發生在哪裏?堆棧跟蹤也許? – 2010-08-18 20:14:11

+0

剛剛更新以包含它。 – 2010-08-18 20:18:38

回答

6

嘗試在main_controller.rb文件的頂部添加

# encoding: UTF-8
。如果這有效,你在處理源文件中的非美國ASCII字符。

在Ruby 1.9,我們正在處理的三種編碼上下文:

  • 源代碼編碼:字符串在源文件中解釋爲默認的US-ASCII,除非魔評論我上面所列內容存在。
  • 外部編碼:假設文本文件中的字符與環境編碼相同。但是可以指定要使用的編碼。例如:open(mydata.txt,「r:UTF-8」)。
  • 內部編碼:指定從文件中讀取文本數據的方式。默認情況下,這是零,這意味着它將與用於讀取它的編碼相同。如果需要不同的東西,可以在IO.open中指定。如:開(mydata.txt, 'R:UTF-8:UTF-16LE')

欲瞭解更多信息,我會閱讀編碼James Edward Gray II's great articles

+1

謝謝,但我在源文件中沒有任何非US ASCII字符。我認爲這肯定是一個乘客問題(因爲這些項目在雜種下運行得很好)。 – 2010-08-19 15:24:41

1

歡迎來到強制字符串編碼的美妙世界;錯誤是Ruby 1.9.x與Ruby 1.8.x在字符串中的行爲差異。

檢查http://blog.phusion.nl/2009/02/02/getting-ready-for-ruby-191/ - 可能有幫助。你可能只需要更新你的gemset。

+0

那麼這是乘客寶石的問題嗎?使用內置的Mongreal(腳本/服務器),應用程序似乎運行良好。也似乎沒有解釋爲什麼3.0.0 RC應用程序正在工作。有任何想法嗎? – 2010-08-18 20:58:13

2

我同意pjmorse有關環境變量的原因,特別是在我的Passenger/Rails設置中,罪魁禍首是LANG值。

當通過腳本/服務器啓動我的Rails應用程序時,我有LANG = en_CA.UTF-8,但在使用Passenger提升應用程序時沒有。

解決方案: 修改乘客配置與包裝開始紅寶石,看到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" "[email protected]" 

注Your_Default_Ruby_Path是無論是在HTTP的PassengerRuby值。在設置包裝之前進行配置。

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 /默認/區域設置(其中包含LANG="en_US.UTF-8")解決了我的問題,而不必爲我的紅寶石做一個包裝。

+0

這個答案適用於我在Ubuntu 12.04上運行的ruby 1.9.3中的rails 3.2應用程序。在應用程序中使用的某些寶石的gemspec中有UTF-8編碼的字符。 – 2012-11-30 17:10:01

+1

現貨!謝謝你,節省了很多痛苦。 – 2012-12-21 10:32:33

相關問題