Rails 和 Ruby 1.9 中的无效多字节字符 (US-ASCII)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1739836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
invalid multibyte char (US-ASCII) with Rails and Ruby 1.9
提问by Tam
I'm using Ruby 1.9.1 with Rails 2.3.4 My application is to handle text input
我使用 Ruby 1.9.1 和 Rails 2.3.4 我的应用程序是处理文本输入
If I try something like (the inside quotation marks look different)
如果我尝试类似(内部引号看起来不同)
text = "”“"
I get the following error:
我收到以下错误:
#<SyntaxError: /Users/tammam56/rubydev/favquote/lib/daemons/twitter_quotes_fetch.rb:54: invalid multibyte char (US-ASCII)
/Users/tammam56/rubydev/favquote/lib/daemons/twitter_quotes_fetch.rb:54: invalid multibyte char (US-ASCII)
/Users/tammam56/rubydev/favquote/lib/daemons/twitter_quotes_fetch.rb:54: syntax error, unexpected $end, expecting keyword_end
I need to user those quotation marks as users might input them and I have to account for that?
我需要使用这些引号,因为用户可能会输入它们,而我必须考虑到这一点?
Any ideas?
有任何想法吗?
回答by Jarek Zmudzinski
Have you tried adding a magic comment in the script where you use non-ASCII chars? It should go on top of the script.
您是否尝试在使用非 ASCII 字符的脚本中添加魔术注释?它应该在脚本之上。
#!/bin/env ruby
# encoding: utf-8
It worked for me like a charm.
它对我来说就像一种魅力。
回答by Shamu
If you want to add magic comments on all the source files of a project easily, you can use the magic_encodinggem
如果你想轻松地在一个项目的所有源文件上添加魔法注释,你可以使用magic_encodinggem
sudo gem install magic_encoding
then just call magic_encodingin the terminal from the root of your app.
然后只需magic_encoding从应用程序的根目录调用终端即可。
回答by Ismoh
I just want to add my solution:
I use german umlauts like ?, ü, ? and got the same error.
@Jarek Zmudzinski just told you how it works, but here is mine:
我只想添加我的解决方案:
我使用德语元音变音,如 ?, ü, ? 并得到同样的错误。
@Jarek Zmudzinski 刚刚告诉你它是如何工作的,但这是我的:
Add this code to the top of your Controller: # encoding: UTF-8
(for example to use flash message with umlauts)
将此代码添加到您的控制器顶部:(# encoding: UTF-8
例如使用带有变音符号的 flash 消息)
example of my Controller:
我的控制器示例:
# encoding: UTF-8
class UserController < ApplicationController
Now you can use ?, ? ,ü, ?, "", etc.
现在您可以使用 ?, ? ,ü, ?, "" 等
回答by Phil Miller
Those slanted double quotes are not ASCII characters. The error message is misleading about them being 'multi-byte'.
那些倾斜的双引号不是 ASCII 字符。错误消息误导了它们是“多字节”。
回答by Nowaker
Just a note that as of Ruby 2.0 there is no need to add # encoding: utf-8. UTF-8 is automatically detected.
请注意,从 Ruby 2.0 开始,无需添加# encoding: utf-8. UTF-8 被自动检测。
回答by cassioscabral
That worked for me:
这对我有用:
$ export LC_ALL=en_US.UTF-8
$ export LANG=en_US.UTF-8

