本文介绍了Rails utf-8问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述

我在那里,我是新的ruby(和rails)和使用瑞典字母在字符串中有som问题。在我的操作中,创建一个如下的实例变量:

  @title =Välkommen

我收到以下错误:

 无效的多字节字符(US-ASCII)
语法错误,意外的$ end,expecting keyword_end
@title =Välkommen
^
pre>

发生了什么?



EDIT:如果我添加:

 #coding:utf-8 


b $ b

在我的控制器的顶部工作。为什么这样,我怎么能爱上这个问题?

解决方案

请参阅Joel spolsky的文章。



这就是为什么你必须告诉ruby你的文件中使用什么编码。由于编码没有标记在与您的文件相关联的某种元数据中,一些软件假设ASCII,直到它知道得更好。 Ruby 1.9可能会这样,直到你的评论时,它会停止,并重新开始读取该文件,现在解码为utf-8。



显然,如果你对ruby文件使用了一些其他的Unicode编码或一些局部编码,你需要改变注释以指示正确的编码。


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"?

解决方案

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.

这篇关于Rails utf-8问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 07:15