为什么 Rails 3 对 UTF-8 字符编码很不满?
我刚刚开始开发一个新的 Rails 应用程序,使用明亮闪亮的新版本 Rails 3.2.1。之前我只使用过3.0.9版本。在描述我的错误之前,请告知我在 Windows 7 32 位上使用 Ruby 版本 ruby 1.9.2p290 (2011-07-09) [i386-mingw32]
。我最近没有更改过 Ruby 版本。我正在使用 Notepad++ v5.9.3 并且没有(故意)更改任何默认设置。
当我第一次运行我的新应用程序时,我收到一条奇怪的消息:
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”可以轻松修复此问题,但正如我所说,我以前从未这样做过。
另一个奇怪的事情是,当我使用生成器时,即使 Rails 生成的文件也是使用 ANSI 编码生成的。总的来说,我很困惑,我想确保我使用良好的编程实践。
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>.
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的。 Rails 3.0+(我认为)要求所有模板都以 UTF-8 编码保存。您需要将文件保存为 UTF-8。如果仍然不起作用,请通过在
.rb
文件的第一行添加以下内容来显式设置编码:将此添加到
.erb
文件的第一行模板:请参阅此相关问题 和 这个类似的问题。在我看来,自从您最初创建文件以来,您的编辑器的编码设置发生了变化。
这很奇怪,我不确定我对此有什么好的建议,除了尝试将
Encoding.default_external = "UTF-8"
添加到您的config.ru< /code> 和 config/environment.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:Add this to the first line of your
.erb
templates:See this related question, and this similar problem. Sounds to me like your editor's encoding settings changed since you originally created the files.
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 yourconfig.ru
andconfig/environment.rb
files.我尝试过
encoding: utf-8
方法,但没有成功,但当我使用 Notepad++ 更改编码时解决了问题。谢谢!I had tried the
encoding: utf-8
method without luck, but I resolved the issue when I changed the encoding using Notepad++. Thanks!