Rails UTF-8文字化け対策

デフォルトのキャラセットをUTF-8として、HTTPヘッダの送信。
app/controllers/application.rbの内容を、下記に差し替えです。
いつも必ずやっとくべきですね。

class ApplicationController < ActionController::Base
 before_filter :configure_charsets

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

(参考)
Getting Unicode, MySql, and Rails to Cooperate
http://ruphus.com/blog/2005/06/23/getting-unicode-mysql-and-rails-to-cooperate/

関連する記事

コメントを書き込む

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <img localsrc="" alt="">