UTF-8 以外のエンコーディングで render
Rails3 は基本 UTF-8 縛り。だけど、外部との通信が絡むページとかだと cp932 とかで出力しないといけなかったりする。
そこで ApplicationController あたりに対応コードを追加する。
def render_with_encoding(*options) if options[-1].is_a?(Hash) && (encoding = options[-1][:encoding]) headers["Content-Type"] = "text/html; charset=#{encoding}" render_without_encoding :text => render_to_string.encode(encoding, :invalid => :replace) end end alias_method_chain :render, :encoding
これで普通の HTML に関しては下のようにしていけるんだけど、Content-Type 決め打ちなんで他のフォーマットに適用するのが難しい。
render :action => 'new', :encoding => 'cp932'
まあ今の用途には十分すぎるんでいいか。