Rails 和 UTF-8 编码
我知道这是编程史上最常见的问题,但我真的相信我现在已经尝试过了,但我仍然得到:不兼容的字符编码:UTF-8 和 ASCII-8BIT
在我的页面中# index.html 文件。
我正在运行 Ruby 1.9.2 和 Rails 3.1.1。
这是我已经验证/测试/设置的:
在database.yml中,设置编码:utf8
开发: 适配器:mysql 编码:utf8 重新连接:假 数据库:kopa_development 泳池:5 用户名:根 密码: 主机:localhost
使用 Notepad++ 检查术语中的文本文件是否编码为 utf-8
- 使用 mysql 查询浏览器检查数据库设置为 utf-8,因此
- 我在控制器中尝试使用 #coding: utf-8 与我要查看的视图相关的列也是如此。
- 我已将 SET NAMES utf8 放入 mysql
- 我尝试过 Encoding.default_internal = Encoding::UTF_8 inenvironment.rb 以及 Encoding.default_external = Encoding::UTF_8
- 我在 application.rb 中有 config.encoding = "utf-8"
- 我已用 Google 搜索
- 我已阅读 http://www.joelonsoftware.com/articles/Unicode.html
- 我已经尝试过 Notepad++ 和 RubyMine
- 添加 .force_encoding 可以工作,但做起来很不方便
有趣的是,如果我设置将index.html.erb转换为ANSI,它会抱怨application.html.erb没有正确的编码(而不是index.html.erb)。我不知道这意味着什么,但这让我觉得编辑有问题。
我记得在使用电子文本编辑器时,我可以设置两件事(参考编码)编码和其他东西。那个“其他”我不记得它是什么了,而且我不再有 e 了。
关于错误消息,我是否正确理解第一个提到的意味着数据库编码,第二个意味着Rails(?)编码?我也看到了相反的错误消息(先是 ASCII,后是 UTF-8)。
编辑:今天早上我用谷歌搜索了一些,发现 mysql-gem 有问题。这些文章有点旧,我尝试了那里的解决方案(例如尝试 mysql2 和 ruby-mysql),但运气不佳。
我完全没有主意了。你呢?
I know this is the most common problem in the history of programming but I reallu believe I have tried it all now but I still get: incompatible character encodings: UTF-8 and ASCII-8BIT
in my pages#index.html file.
I am running Ruby 1.9.2 and Rails 3.1.1.
This is what I have verified / tested / set:
In database.yml, set encoding: utf8
development:
adapter: mysql
encoding: utf8
reconnect: false
database: kopa_development
pool: 5
username: root
password:
host: localhostChecked with Notepad++ that the textfile(s) in term is encoded to utf-8
- Checked with mysql query browser that the database is set to utf-8, so are the columns
- I have tried with # coding: utf-8 in the controller related to the view I am trying to look at.
- I have put SET NAMES utf8 in mysql
- I have tried Encoding.default_internal = Encoding::UTF_8 in environment.rb as well as Encoding.default_external = Encoding::UTF_8
- I have config.encoding = "utf-8" in application.rb
- I have Googled
- I have read http://www.joelonsoftware.com/articles/Unicode.html
- I have tried both Notepad++ and RubyMine
- Adding .force_encoding works but is very inconvenient to do
A funny thing is that if I set the index.html.erb to ANSI it will complain that application.html.erb does not have the correct encoding (rather than index.html.erb). I don't know what this means but it makes me think that there is something fishy with the editor.
I remember when using e text editor that there were two things I could set (with reference to encoding) the encoding and something else. That "else" I don't remember what it was though and I don't have e anymore.
Regarding the error-message, do I understand it correctly that the first mentioned means the database encoding and the second the Rails(?) encoding? I have seen the opposite error message as well (ASCII first and UTF-8 after).
Edit: I Googled some more this morning and found there is something wrong with the mysql-gem. The articles were a bit old and I tried the solutions there (such as trying mysql2 & ruby-mysql) with no better luck.
I am all out of ideas. How about you?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我无法解决这个问题。我通过购买 Mac 并继续在其上开发我的 Rails 应用程序来“解决”了这个问题......
编码 - 我:1-0
I couldn't fix this problem. I "solved" it by purchasing a Mac and continue to develop my Rails apps on it instead...
Encoding - me: 1-0
我能够克服这个错误,但不幸的是我在yield语句(application.html.erb)中遇到了同样的错误。半小时后我放弃了。
我将一些 erb 文件从带有 BOM 的 utf-8 保存为 utf-8 无 BOM。混合编码不好,我认为这是您错误的原因。
我还发现了具有不同行结尾的文件:lf 或 crlf。
如果您在 Windows 中进行开发,我建议您安装 virtual box 来设置 Linux 虚拟机。您可以通过 samba 访问文件系统,这对我来说非常有用。
另外,将文本编辑器配置为使用不带 BOM 的 utf-8 和 lf 行尾。
I was able to overcome that error but unfortunately I got the same error in yield statement (application.html.erb). After an half hour I gave up.
I saved some erb files from utf-8 with BOM to utf-8 without BOM. Mixing encoding is not good and I think it's the reason for your errors.
I also found files with different line ending: lf or crlf.
If you are developing in Windows, I recommend you the installation of virtual box to setup a linux virtual machine. You can access file system through samba, it works pretty good for me.
Also, configure your text editor for using utf-8 without BOM and lf end of line.