Rails+MySQL报错incompatible character encodings: ASCII-8BIT and UTF-8

33 views
Skip to first unread message

Bachue

unread,
Sep 20, 2011, 12:26:27 AM9/20/11
to sh...@googlegroups.com, shangha...@googlegroups.com
Rails项目,后台用的是MySQL数据库,表在创建的时候用的也是UTF-8编码
报错说: incompatible character encodings: ASCII-8BIT and UTF-8

附件中是gemfile以及database.yml

谢谢!
Gemfile
database.yml

Sam Qiu

unread,
Sep 20, 2011, 12:32:54 AM9/20/11
to shangha...@googlegroups.com, sh...@googlegroups.com
database.yml 里的密码加引号看看?

2011/9/20 Bachue <bachu...@gmail.com>
--
--~--~---------~--~----~------------~-------~--~----~
Shanghai Ruby&Rails 爱好者论坛是一个非营利的民间组织,它旨在为Ruby&Rails爱好者提供一个自由,开放的交流平台,论坛每季度举行一次大型会议,会议的所有费用由赞助商支付,来宾不需交纳任何费用。
We push the china ruby&rails community forward!!
Shanghai Ruby&Rails的邮件列表地址是 shangha...@googlegroups.com
要了解更多详情,请访问 http://groups.google.com/group/shanghaionrails?hl=en?hl=en
退订请发送mail到 shanghaionrai...@googlegroups.com
--~--~---------~--~----~------------~-------~--~----~

Bachue

unread,
Sep 20, 2011, 12:36:38 AM9/20/11
to shangha...@googlegroups.com
这个貌似和我的问题没有什么关系啊。

2011/9/20 Sam Qiu <kins...@gmail.com>
database.yml 里的密码加引号看看?

菁雷

unread,
Sep 20, 2011, 12:44:39 AM9/20/11
to shangha...@googlegroups.com
错误的详细信息是什么

在 11-9-20,Bachue<bachu...@gmail.com> 写道:


> 这个貌似和我的问题没有什么关系啊。
>
> 2011/9/20 Sam Qiu <kins...@gmail.com>
>
>> database.yml 里的密码加引号看看?
>

sharp

unread,
Sep 20, 2011, 12:46:20 AM9/20/11
to shangha...@googlegroups.com
gem 'mysql2'

--

Bachue

unread,
Sep 20, 2011, 12:49:43 AM9/20/11
to shangha...@googlegroups.com
请见: http://paste.ubuntu.org.cn/89070

2011/9/20 菁雷 <raykinc...@gmail.com>
错误的详细信息是什么

Bachue

unread,
Sep 20, 2011, 12:51:49 AM9/20/11
to shangha...@googlegroups.com
使用gem 'mysql2' 后错误信息请见:http://paste.ubuntu.org.cn/89071
里面提到的gem install activerecord-mysql-adapter,但是不存在这个gem。

2011/9/20 sharp <sh...@theplant.jp>

菁雷

unread,
Sep 20, 2011, 12:55:43 AM9/20/11
to shangha...@googlegroups.com
是刚开始做的新项目吗?
你的数据库是用sql命令创建的还是rake命令创建的

在 11-9-20,Bachue<bachu...@gmail.com> 写道:

Vincent Tsao

unread,
Sep 20, 2011, 12:59:23 AM9/20/11
to shangha...@googlegroups.com
controller里是否有中文? 如果有的话,建议在对应controller最上面加上这一句

# encoding: utf-8

Bachue

unread,
Sep 20, 2011, 1:00:47 AM9/20/11
to shangha...@googlegroups.com
两种方法都尝试过了,都一样。

2011/9/20 菁雷 <raykinc...@gmail.com>:

Bachue

unread,
Sep 20, 2011, 1:01:53 AM9/20/11
to shangha...@googlegroups.com
controller 里没有中文,中文在一个partial layout里。

2011/9/20 Vincent Tsao <caojun...@gmail.com>:

Vincent Tsao

unread,
Sep 20, 2011, 1:05:55 AM9/20/11
to shangha...@googlegroups.com
加上mysql2的版本号

gem 'mysql2', '0.2.6'

wxianfeng

unread,
Sep 20, 2011, 1:06:43 AM9/20/11
to shangha...@googlegroups.com
所有文件都加上#coding:utf-8 吧 



2011/9/20 Bachue <bachu...@gmail.com>

HoLin

unread,
Sep 20, 2011, 1:41:01 AM9/20/11
to shangha...@googlegroups.com
再不行, force_encoding
<%= title.force_encoding('utf-8') %>

2011/9/20 wxianfeng <wang....@gmail.com>



--
Regards
HoLin

sharp

unread,
Sep 20, 2011, 1:47:10 AM9/20/11
to shangha...@googlegroups.com
这种处理问题的态度是不可取的

Bachue

unread,
Sep 20, 2011, 2:04:33 AM9/20/11
to shangha...@googlegroups.com
加版本号和不加版本号获得的错误消息是一样的。

2011/9/20 Vincent Tsao <caojun...@gmail.com>:
> 加上mysql2的版本号

Vincent Tsao

unread,
Sep 20, 2011, 2:27:06 AM9/20/11
to shangha...@googlegroups.com
卸载了之前的mysql2 gem看看,只安装 0.2.6版本的


2011/9/20 Bachue <bachu...@gmail.com>
加版本号和不加版本号获得的错误消息是一样的。

2011/9/20 Vincent Tsao <caojun...@gmail.com>:
> 加上mysql2的版本号

Bachue

unread,
Sep 20, 2011, 2:34:08 AM9/20/11
to shangha...@googlegroups.com
依旧出现相同的错误信息

Gemfile.lock: http://paste.ubuntu.org.cn/89084

2011/9/20 Vincent Tsao <caojun...@gmail.com>:

菁雷

unread,
Sep 20, 2011, 2:42:51 AM9/20/11
to shangha...@googlegroups.com
mysql2 小于3.0的版本只支持rails3.1以下。所以3.1要是用mysql2 3.0以上的版本
下面摘自mysql2的说明
NOTE: as of 0.3.0, and ActiveRecord 3.1 - the ActiveRecord adapter has
been pulled out of this gem and into ActiveRecord itself. If you need
to use mysql2 with Rails versions < 3.1 make sure and specify gem
"mysql2", "~> 0.2.7" in your Gemfile

我刚刚新建了一个3.1项目,用的是mysql2,并没有报安装错误,启动server也是成功的,所以我不明白为什么你的mysql2安装不了的

在 11-9-20,Bachue<bachu...@gmail.com> 写道:

Bachue

unread,
Sep 20, 2011, 2:59:46 AM9/20/11
to shangha...@googlegroups.com
解决了,问题在于当Gemfile中我用了mysql2之后,config/database.yml中的adapter也应该改为mysql2,否则就出现如上错误。

非常感谢大家的帮助。

2011/9/20 菁雷 <raykinc...@gmail.com>:

Reply all
Reply to author
Forward
0 new messages