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
?
?
?
但是用浏览器访问时就是卡死,不知道为什么require 'mysql'class Mysql::Result def encode(value, encoding = "utf-8") String === value ? value.force_encoding(encoding) : value end def each_utf8(&block) each_orig do |row| yield row.map {|col| encode(col) } end end alias each_orig each alias each each_utf8 def each_hash_utf8(&block) each_hash_orig do |row| row.each {|k, v| row[k] = encode(v) } yield(row) end end alias each_hash_orig each_hash alias each_hash each_hash_utf8end