2007年6月28日 星期四

Ruby 連mysql 使用utf-8

1.找 app/config/database.yml 修改成
development:
adapter: mysql
database: dbname
username: username
password: password
host: ip port: 3306
encoding: utf8

2.找app/config/environment.rb最上面加$KCODE="u"

3.找app/controllers/environment.rb修改成
class ApplicationController < ActionController::Base
# Pick a unique cookie name to distinguish our session data from others'
session :session_key => '_testmysql_session_id'

before_filter :set_charset
def set_charset
@headers["Content-Type"] = "text/html; charset=utf-8"
end
suppress(ActiveRecord::StatementInvalid) do
ActiveRecord::Base.connection.execute "SET NAMES UTF8"
end

end

1 則留言:

winson 提到...

不需要這麼麻煩,直接定義在mysql資料庫的my.inf即可。