rails3 mysql2安装,incompatible character encodings: UTF-8 and ASCII-8BIT 解决!

问题环境:

os:  windows XP

 

F:\>ruby -v

ruby 1.9.2p180 (2011-02-18) [i386-mingw32]

 

 

 

F:\>rails -v

Rails 3.0.5

 

 

问题描述:

1、安装不了mysql2

      rails3 默认使用mysql adapter是mysql2. 

   1) gem install mysql2

         ERROR:  Error installing mysql2:

         The 'mysql2' native gem requires installed build tools.

 

        Please update your PATH to include build tools or download the DevKit

        from 'http://rubyinstaller.org/downloads' and follow the instructions

        at 'http://github.com/oneclick/rubyinstaller/wiki/Development-Kit'

 

   2) 安装DevKit  按照http://wuhuizhong.iteye.com/blog/875493

 

   3)gem install mysql2

         Temporarily enhancing PATH to include DevKit...

        Building native extensions.  This could take a while...

        ERROR:  Error installing mysql2:

        ERROR: Failed to build gem native extension.

 

        F:/Ruby192/bin/ruby.exe extconf.rb

       checking for rb_thread_blocking_region()... yes

       checking for main() in -llibmysql... no

         *** extconf.rb failed ***

Could not create Makefile due to some reason, probably lack of

necessary libraries and/or headers.  Check the mkmf.log file for more

details.  You may need configuration options.

 

    4)这样问题就同 http://www.iteye.com/problems/49494

    5)那就暂且使用mysql  adapter吧

       gemfile 中改为: gem 'mysql', '2.8.1'

2.incompatible character encodings: UTF-8 and ASCII-8BIT

    当页面中写了中文之后,比如一个label, <%= f.label :title, '标题' %>

    按错误信息google ,baidu了 n多,都没有解决。包括:

    1) html.erb文件前加这一行:<%# encoding:utf-8 %> ,并保存成了utf-8格式(这一点是需要的)

    2) http://www.iteye.com/topic/355909 这个第一点试过了,第二点找不到类似的文件

 

    后来发现 new页面可以正常显示,edit页面报这个错,同是一个_form.html.erb

 

    按一篇文章中说法,测试了下:

   页面代码:

   <td><%= blog.title.encoding %></td>

 

    页面显示:ASCII-8BIT

    说明:因为rails从数据库取出的中文默认编码是ASCII-8BIT,如果页面是UTF-8编码,  concat就会出现这个错误。

 

  要解决这个问题,就需要“数据库取出的中文 转为utf-8”. 找不到可用的资料。

 

解决

    感谢QQ网友 寿司   告诉我windows下安装mysql2的命令:

    gem install mysql2 -v 0.2.6 --platform x86-mingw32

 

    成功安装了mysql2,  将gemfile中改为:gem 'mysql2', '0.2.6'  

    database.yml 中adapter 改回mysql2

 

    再次测试:页面代码:<td><%= blog.title.encoding %></td>

    result:  UTF-8.

 

    第2个问题也迎刃而解了。再次感谢 寿司。


 

补充:

   看来mysql2 adapter还是有bug, 一个表有5列:

  A: id   B:string  C:integer  D:string  E:string

  从数据库查出,B列utf-8, 显示正常, D,E  却是ASCII-8BIT。

 

   我重建了数据库表,调整了列的顺序,改为:

   A: id   B:string   D:string  E:string C:integer 

  从数据库查出,B,D列utf-8, 显示正常, E  还是ASCII-8BIT。

 

    再重建一次,,改为:

   A: id   B:string   D:string  E:string C:integer 

   这次,从数据库查出,B,D,E列utf-8,都显示正常了

 

2011-04-17 补充:

  今天遇到了这个错误:invalid date   

  http://hot88zh.iteye.com/blog/858300

  按以上博文中的方法:复制Mysql5 server端的libmysql.dll文件,到Ruby安装目录下的bin文件夹中,即可解决~

 

  之前使用mysql adapter时,ruby bin下的libmysql.dll 是一个早期版本,现在用mysql2 adapter,就需要用mysql 5 server下的libmysql.dll

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值