Rails utf-8 问题

发布于 2024-12-01 02:59:48 字数 424 浏览 0 评论 0原文

我在那里,我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

人│生佛魔见 2024-12-08 02:59:48

请参阅 Joel spolsky 的文章“每个软件开发人员绝对必须了解 Unicode 和字符集的绝对最低限度(没有任何借口!)”。

引用简洁回答这个问题的部分

关于编码的最重要的事实

如果你完全忘记了我刚才解释的一切,请记住
一个极其重要的事实。 拥有字符串是没有意义的
不知道它使用什么编码。
你不能再坚持你的
把头埋在沙子里,假装“纯”文本是 ASCII。

这就是为什么您必须告诉 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

The Single Most Important Fact About Encodings

If you completely forget everything I just explained, please remember
one extremely important fact. It does not make sense to have a string
without knowing what encoding it uses.
You can no longer stick your
head in the sand and pretend that "plain" text is ASCII.

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.

少女的英雄梦 2024-12-08 02:59:48

Ruby 1.9(Rails 3 的基础)中的“神奇注释”告诉解释器需要什么编码。这很重要,因为在 Ruby 1.9 中,每个字符串都有一个编码。在 1.9 之前,每个字符串只是一个字节序列。

James Gray 关于 Ruby 和 Unicode 的系列博客文章对此问题进行了很好的描述。与您的问题完全相关的是 http://blog.grayproducts.net/articles/ruby_19s_ Three_default_encodings(但是看看其他人,因为他们非常好)。

文章中的重要内容:

第一个是源编码的主要规则:源文件接收 US-ASCII 编码,除非您另有说明。

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:

The first is the main rule of source Encodings: source files receive a US-ASCII Encoding, unless you say otherwise.

薄荷港 2024-12-08 02:59:48

有几个地方可能会导致 utf-8 编码出现问题。
但有一些技巧可以解决这个问题:

  1. 确保项目中的每个文件都是基于 utf-8 的(如果您
    使用 rad Rails,这很容易完成:标记您的项目,
    选择属性,在“文本文件编码”框中选择“其他:
    utf-8")

请务必再次在文件中放入奇怪的“å,ä,ö”字符
否则你会得到一个 mysql 错误,因为它会将你的“å,ä,ö”更改为
“square”(未知字符)

  1. 在为每个服务器环境设置的databases.yml中(在此
    使用 mysql 的“开发”示例)

    <前><代码>开发:
    适配器:mysql
    编码:utf8

  2. 在应用程序控制器中设置前置过滤器
    (应用程序.rb):

    类ApplicationController <动作控制器::基础
     before_filter :set_charset
     def set_charset
      @headers["Content-Type"] = "text/html; charset=utf-8"
     结尾
    结尾
    
  3. 一定要在你的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:

  1. make sure that every file in your project is utf-8 based (if you
    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)

  1. in your databases.yml set for each server environment (in this
    example "development" with mysql)

    development:
       adapter: mysql
       encoding: utf8
    
  2. set a before filter in your application controller
    (application.rb):

    class ApplicationController < ActionController::Base
     before_filter :set_charset
     def set_charset
      @headers["Content-Type"] = "text/html; charset=utf-8"
     end
    end
    
  3. 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 )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文