问题
I have a problem running a Ruby on Rails application using Passenger. My application resides in the /Users/Glutexo/Práce/Bytek/Bytek directory, which you can see has a non-ASCII character in in (á). Even when I symlink it into another directory, e.g. /Library/WebServer/Documents/Bytek with all characters being US-ASCII, the problem is still there.
But when I create another Rails application physically in all US-ASCII path, like /Users/Glutexo/rails/pokus, it works: The application starts and runs normally through Passenger.
The error page I get when trying to run a Rails application residing in non-ASCII path says the following:
Error message:
invalid byte sequence in US-ASCII
Exception class:
ArgumentError
Backtrace:
0 | /Users/Glutexo/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/pathname.rb | 45 | in `=~'
The line in pathname.rb mentioned is in the following function:
# chop_basename(path) -> [pre-basename, basename] or nil
def chop_basename(path)
base = File.basename(path)
if /\A#{SEPARATOR_PAT}?\z/o =~ base # This is the line no. 45
return nil
else
return path[0, path.rindex(base)], base
end
end
private :chop_basename
Does anyone have any suggestion, how to convince Passenger to be able to run Ruby on Rails application even from a path containing non-ASCII characters?
Rails is version 3.2.2, Ruby is version 1.9.3-p125, Apache is version 2.2.21, Passenger is version 3.0.12.
Thanks in advance for any help!
回答1:
The solution to this problem is to add a 'magic comment' to your rails app source files. For example, at the beginning of a file that will encounter a non-ascii character, add the following:
# encoding: utf-8
There's a useful gem out there to help you do this: https://github.com/m-ryan/magic_encoding
To confirm this solution, I setup passenger with a rails app in a directory containing 'Práce' and got a similar error to you. I ran the magic_encoding gem's 'magic_encoding' command in the root of my rails app. I restarted passenger and it seems to have done the trick. Hope that helps!
See also: invalid multibyte char (US-ASCII) with Rails and Ruby 1.9
来源:https://stackoverflow.com/questions/10175975/ruby-on-rails-application-won-t-start-using-passenger-when-there-are-non-ascii-c