Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RVM, Ruby 1.9.2, Rails 2.3.8, Passenger and "invalid byte sequence in US-ASCII"

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:

invalid byte sequence in US-ASCII

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'
like image 889
Kevin Sylvestre Avatar asked Aug 18 '10 19:08

Kevin Sylvestre


5 Answers

Try adding

# encoding: UTF-8
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.

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

  • Source code encoding: Strings in a source file are interpreted as US-ASCII by default, unless the magic comment I list above is present.
  • External encoding: The characters in a text file are assumed to be in the same encoding as the environment. However one can specify the encoding to use. Eg: open(mydata.txt, "r:UTF-8").
  • Internal encoding: That specifies how the text data is encoded once read from a file. By default this is nil, meaning it will be the same as the encoding used to read it. If something different is needed, it can be specified in IO.open. Eg: open(mydata.txt, 'r:UTF-8:UTF-16LE')

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

like image 135
Bernard Avatar answered Nov 02 '22 15:11

Bernard


I had similar issues on Ubuntu (11.10) because this was in my /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

Swapping the comments out on this one to use the /etc/default/locale (which contains LANG="en_US.UTF-8") resolved the issue for me without having to make a wrapper for my ruby.

like image 29
Gabe Martin-Dempesy Avatar answered Nov 02 '22 15:11

Gabe Martin-Dempesy


I had a similar issue with a different server, and environment variables turned out to be the culprit.

like image 20
pjmorse Avatar answered Nov 02 '22 16:11

pjmorse


I agree with pjmorse regarding the environment variable cause, specially in my Passenger/Rails set-up, the culprit was the LANG value.

When starting my Rails app through script/server, I had LANG=en_CA.UTF-8, but not when raising the app with Passenger.

Solution: Modify the Passenger configuration to start Ruby with a wrapper, see http://blog.phusion.nl/2008/12/16/passing-environment-variables-to-ruby-from-phusion-passenger/

Use this as a wrapper:

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

Note Your_Default_Ruby_Path is whatever was in the PassengerRuby value of the http.conf before you set-up the wrapper.

like image 2
Jason Sweeney Avatar answered Nov 02 '22 16:11

Jason Sweeney


Welcome to the wonderful world of forced string encoding; the error is a Ruby 1.9.x vs Ruby 1.8.x behavior difference in strings.

Check http://blog.phusion.nl/2009/02/02/getting-ready-for-ruby-191/ -- might be helpful. You probably just need to update your gemset.

like image 1
todb Avatar answered Nov 02 '22 16:11

todb