要确定你的mysql数据库用的是什么字符集,可以通过命令来看
mysql> show create database <你的数据库名>
可以用下面的命令建立一个gbk字符集的数据库
mysql> create database xxxx default character set gbk
要确定你连接的的字串encode,比如在config/database.yml中
development:
adapter: mysql
database: collector
encoding: gbk
username: root
password:
host: localhost
在config/enviroment.rb 最后面增加一行
$KCODE=”gbk”
修改MySQL的配置文件C:\Program Files\MySQL\MySQL Server 5.0\my.ini
default-character-set=utf8(共两处,这里也可以在安装数据库的时候就选择好)
修改ApplicationController
class ApplicationController < ActionController::Base
before_filter :configure_charsets
def configure_charsets
# @headers["Content-Type"] = “text/html; charset=utf-8″
@response.headers["Content-Type"] = “text/html; charset=utf-8″
# Set connection charset. MySQL 4.0 doesn??t support this so it
#will throw an error, MySQL 4.1 needs this
suppress(ActiveRecord::StatementInvalid) do
ActiveRecord::Base.connection.execute ‘SET NAMES utf8′
end
end
end
在所有的具体页面(rhtml)或者模板文件(layout)开头加上页面编码
Rails has a helper method,
going to want to use it when substituting values into HTML pages.
Email: <%= h(“Ann & Bill <[email protected]>”)%>
In this example, the h() method prevents the special characters in the
e-mail address from garbling the browser display—they’ll be escaped as
Email: Ann & Bill <[email protected]>—the
HTML entities. The browser sees
special characters are displayed appropriately.
Module ERB::Util
In: lib/erb.rbA utility module for conversion routines, often handy in HTML generation.
Methods
h html_escape u url_encode
Public Instance methodsh(s)
Alias for html_escape
html_escape(s)A utility method for escaping HTML tag characters in s.
require “erb”
include ERB::Utilputs html_escape(“is a > 0 & a < 10?”)
Generates
is a > 0 & a < 10?
[Source]
# File lib/erb.rb, line 780
780: def html_escape(s)
781: s.to_s.gsub(/&/, “&”).gsub(/\”/, “"”).gsub(/>/, “>”).gsub(/</, “<”)
782: endu(s)
Alias for url_encode
url_encode(s)A utility method for encoding the String s as a URL.
require “erb”
include ERB::Utilputs url_encode(“Programming Ruby: The Pragmatic Programmer’s Guide”)
Generates
Programming Ruby : The Pragmatic Programmer’s Guide
学习一门新语言,真的太不容易了,首先难过的一关,就是写代码的工具——IDE。
关于Ruby和Rails的东西,在google和百度上面搜索下,竟然出来可数的页数,从头到尾翻看了一遍,把所有有关IDE的文章看了下,然后看见一个就下载一个来装,什么scite、RDE、FreeRIDE、Notepad++、RadRails、Eclips+RDT……所有能用的,我都装了,但是……没有我喜欢的。对ROR的支持,都只是代码着色、测试、Debug,最多再集成下数据库、WEB Server,到此为止了,也许作为一个IDE,做到这些就够了,但是,难道就不可能有一个IDE,像JBuild写Java、VS写C#那样舒服顺手的么?即便是动态语言,一些基本的东西还是可以有代码提示的啊。郁闷。
于是乎,绕了一大圈,最后还是回到Ruby默认安装的SCiTe了……还研究了一会它的配置文件。郁闷、郁闷……
偶尔发现了Dreamweaver的ROR插件,还算顺手,搞到最后,还是用我那看腻了的DW8。还是郁闷。
于是乎,非常期待DW9……中文版赶快出(不出也没有多少关系,因为学ROR,我看的英文资料也不少了,已经锻炼到看英文书不头疼的地步了)最主要的是——不要钱的赶快出!
残念……