Rails utf-8 问题
我在那里,我是 ruby (和 Rails)的新手,在字符串中使用瑞典语字母时遇到一些问题。在我的操作中,创建一个如下所示的实例变量:
@title = "Välkommen"
我收到以下错误:
invalid multibyte char (US-ASCII)
syntax error, unexpected $end, expecting keyword_end
@title = "Välkommen"
^
发生了什么?
编辑:如果我添加:
# coding: utf-8
在控制器的顶部它就可以工作。这是为什么?我该如何解决这个“问题”?
I there, I'm new to ruby (and rails) and having som problems when using Swedish letters in strings. In my action a create a instance variable like this:
@title = "Välkommen"
And I get the following error:
invalid multibyte char (US-ASCII)
syntax error, unexpected $end, expecting keyword_end
@title = "Välkommen"
^
What's happening?
EDIT: If I add:
# coding: utf-8
at the top of my controller it works. Why is that and how can I slove this "issue"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请参阅 Joel spolsky 的文章“每个软件开发人员绝对必须了解 Unicode 和字符集的绝对最低限度(没有任何借口!)”。
引用简洁回答这个问题的部分
这就是为什么您必须告诉 ruby 文件中使用什么编码。由于编码未在与您的文件关联的某种元数据中标记,因此某些软件假定为 ASCII,直到它知道得更好为止。 Ruby 1.9 可能会这样做,直到您发表评论时它才会停止,并重新开始读取文件,现在将其解码为 utf-8。
显然,如果您对 ruby 文件使用了其他 Unicode 编码或更多本地编码,则需要更改注释以指示正确的编码。
See Joel spolsky's article "The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)".
To quote the part that answers this questions concisely
This is why you must tell ruby what encoding is used in your file. Since the encoding is not marked in some sort of metadata associated with your file, some software assumed ASCII until it knows better. Ruby 1.9 probably does so until your comment when it will stop, and restart reading the file now decoding it as utf-8.
Obviously, if you used some other Unicode encoding or some more local encoding for your ruby file, you would need to change the comment to indicate the correct encoding.
Ruby 1.9(Rails 3 的基础)中的“神奇注释”告诉解释器需要什么编码。这很重要,因为在 Ruby 1.9 中,每个字符串都有一个编码。在 1.9 之前,每个字符串只是一个字节序列。
James Gray 关于 Ruby 和 Unicode 的系列博客文章对此问题进行了很好的描述。与您的问题完全相关的是 http://blog.grayproducts.net/articles/ruby_19s_ Three_default_encodings(但是看看其他人,因为他们非常好)。
文章中的重要内容:
The "magic comment" in Ruby 1.9 (on which Rails 3 is based) tells the interpreter what encoding to expect. It is important because in Ruby 1.9, every string has an encoding. Prior to 1.9, every string was just a sequence of bytes.
A very good description of the issue is in James Gray's series of blog posts on Ruby and Unicode. The one that is exactly relevant to your question is http://blog.grayproductions.net/articles/ruby_19s_three_default_encodings (but see the others because they are very good).
The important line from the article:
有几个地方可能会导致 utf-8 编码出现问题。
但有一些技巧可以解决这个问题:
使用 rad Rails,这很容易完成:标记您的项目,
选择属性,在“文本文件编码”框中选择“其他:
utf-8")
请务必再次在文件中放入奇怪的“å,ä,ö”字符
否则你会得到一个 mysql 错误,因为它会将你的“å,ä,ö”更改为
“square”(未知字符)
在为每个服务器环境设置的databases.yml中(在此
使用 mysql 的“开发”示例)
<前><代码>开发:
适配器:mysql
编码:utf8
在应用程序控制器中设置前置过滤器
(应用程序.rb):
一定要在你的mysql中将编码设置为utf-8(我只使用过
mysql ..所以我不知道每个表的其他数据库)。如果你
使用 mySQL Administrator 你可以这样做:编辑表,按
“表选项”选项卡,将字符集更改为“utf8”并将排序规则更改为
“utf8_general_ci”
( Courtsey : kombatsanta )
There are several places that can cause problems with utf-8 encoding.
but some tricks are to solve this problem:
are using rad rails, this is simple to accomplish: mark your project,
select properties, in the "text-file-encoding" box, select "other:
utf-8")
Be sure to put in your strange "å,ä,ö" characters in your files again
or you'll get a mysql error, because it will change your "å,ä,ö" to a
"square" (unknown character)
in your databases.yml set for each server environment (in this
example "development" with mysql)
set a before filter in your application controller
(application.rb):
be sure to set the encoding to utf-8 in your mysql (I've only used
mysql.. so I don't know about other databases) for every table. If you
use mySQL Administrator you can do like this: edit table, press the
"table option" tab, change charset to "utf8" and collation to
"utf8_general_ci"
( Courtsey : kombatsanta )