RailsのUTF8コードの文字化け対策

ここでは、「 RailsのUTF8コードの文字化け対策」 に関する記事を紹介しています。
RailsによるアジャイルWebアプリケーション開発で勉強しながら、Railsアプリを作っているが、Railsをバージョンアップ(ver 1.2.3)しているうちに、いつの間にか本書の記述だけでは文字化けするようになってしまった。

それで、やっとこさ解決方法を見つけた。

app/controllers/application.rbを以下のように修正する(情報源)。

class ApplicationController < ActionController::Base
before_filter :set_charset
before_filter :configure_charsets

# Pick a unique cookie name to distinguish our session data from others'
session :session_key => '_practice_session_id'

private
def set_charset
@headers["Content-Type"] = "text/html; charset=UTF-8"
end

def configure_charsets
@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


なんとなく、コードは理解できるが説明はできない。(-o-)

ブログランキング登録中。
コメント
この記事へのコメント
コメントを投稿する
URL:
Comment:
Pass:
秘密: 管理者にだけ表示を許可する
 
トラックバック
この記事のトラックバックURL
http://konohaotoshi.blog69.fc2.com/tb.php/83-cac6370b
この記事にトラックバックする(FC2ブログユーザー)
この記事へのトラックバック