为什么Rails 3可以适应UTF-8字符编码? [英] Why does Rails 3 pitch a fit about UTF-8 character encoding?

查看:84
本文介绍了为什么Rails 3可以适应UTF-8字符编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用新的Rails 3.2.1明亮而闪亮的版本开发新的Rails应用程序.以前,我最多只使用3.0.9版.在描述错误之前,请知道我在Windows 7 32位系统上使用的是Ruby版本ruby 1.9.2p290 (2011-07-09) [i386-mingw32].我最近没有更改我的Ruby版本.我使用的是Notepad ++ v5.9.3,没有(故意)更改任何默认设置.

I just started work on a new Rails app, using the bright and shiny new version of Rails, 3.2.1. Previously, I had only used up to version 3.0.9. Before I describe my error, let it be known that I am using Ruby version ruby 1.9.2p290 (2011-07-09) [i386-mingw32] on Windows 7 32-bit. I have not changed my version of Ruby recently. I am using Notepad++ v5.9.3 and haven't (on purpose) changed any default settings.

当我第一次运行我的新应用程序时,我收到一条奇怪的消息:

When I ran my new app for the first time, I got an odd message:

ActionView::WrongEncodingError in Index#index

Your template was not saved as valid UTF-8. Please either specify UTF-8 as the encoding for your template in your text editor, or mark the template with its encoding by inserting the following as the first line of the template:

# encoding: <name of correct encoding>.

我不明白为什么突然出现此错误.它是对Rails 3.2.1所做的更改的一部分吗?可以通过进入Notepad ++并使用编码"菜单选项转换为UTF-8"来轻松修复它,但是,就像我说的那样,我从来没有做过此事.

I do not understand why I am getting this error all of a sudden. Is it part of changes made to Rails 3.2.1? It is easily fixed by going into Notepad++ and using the Encoding menu option "Convert to UTF-8" but, like I said, I've never had to do this before.

另一个奇怪的是,当我使用生成器时,即使Rails生成的文件也是使用ANSI编码生成的.总体而言,我很困惑,我想确保自己使用的是良好的编程习惯.

The other odd thing is that even the files that Rails generates are generated with ANSI encoding when I use a generator. Overall, I'm confused and I want to make sure that I'm using good programming practices.

推荐答案

这是对Rails 3.2.1所做的更改的一部分吗?可以通过进入Notepad ++并使用编码"菜单选项转换为UTF-8"来轻松修复它,但是,就像我说的那样,我以前从来没有这样做.

Is it part of changes made to Rails 3.2.1? It is easily fixed by going into Notepad++ and using the Encoding menu option "Convert to UTF-8" but, like I said, I've never had to do this before.

是的. Rails 3.0+(我认为)要求所有模板都必须以UTF-8编码保存.您需要将文件另存为UTF-8.如果仍然无法解决问题,请通过在.rb文件的第一行中添加以下内容来显式设置编码:

Yes. Rails 3.0+ (I think) requires all templates to be saved in UTF-8 encoding. You need to save the file as UTF-8. If that still doesn't work, set the encoding explicitly by adding on the first line of your .rb files the following:

# encoding: utf-8

将其添加到.erb模板的第一行:

Add this to the first line of your .erb templates:

<%# encoding: utf-8 %>

请参见此相关问题此类似问题.在我看来,自您最初创建文件以来,您的编辑器的编码设置已更改.

See this related question, and this similar problem. Sounds to me like your editor's encoding settings changed since you originally created the files.

另一个奇怪的是,当我使用生成器时,即使Rails生成的文件也是使用ANSI编码生成的.总体而言,我很困惑,我想确保自己使用的是良好的编程习惯.

The other odd thing is that even the files that Rails generates are generated with ANSI encoding when I use a generator. Overall, I'm confused and I want to make sure that I'm using good programming practices.

这很奇怪,我不确定对此是否有很好的建议,然后尝试将Encoding.default_external = "UTF-8"添加到您的config.ruconfig/environment.rb文件中.

That is quite odd, and I'm not sure I have a good suggestion for that one, other then trying to add Encoding.default_external = "UTF-8" to your config.ru and config/environment.rb files.

这篇关于为什么Rails 3可以适应UTF-8字符编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆