UTF-8,Ruby on Rails under Ubuntu

I installed Ruby on Rails under Ubuntu(from /var/lib/locales/supported.d/local, I know, my default charset is en_US.UTF-8 UTF-8), and create a demo, by default, mysql (table type is MyISAM)supports UTF-8. But when I input Chinese character and save to database, the output on the web is UTF-8 code, not what I want, why? After searching, I got the answer , I need modify app/controller/application.rb just like this:

class ApplicationController < ActionController::Base
before_filter :set_charset

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

P.S.
My environment:
Server:
OS: Ubuntu 6.06.1 on Vmware(default charset is en_US.UTF-8 UTF-8)
Ruby: 1.8.4
Rails: 1.1.6
Mysql: 5.0.2
Mongrel: 0.3.13.4
Client:
Firefox: 1.5.0.6
IE: 6.0

Share and Enjoy:
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • Wists
  • BlinkList
  • blogmarks
  • blogtercimlap
  • connotea
  • DotNetKicks
  • Fark
  • Fleck
  • Gwar
  • Haohao
  • IndianPad
  • Internetmedia
  • LinkaGoGo
  • MyShare
  • Netscape
  • NewsVine
  • Rec6
  • Reddit
  • Scoopeo
  • Slashdot
  • StumbleUpon
  • Technorati
  • Webride

3 Responses to “UTF-8,Ruby on Rails under Ubuntu

  • 1
    Hugh
    October 24th, 2006 19:18

    It should be working, maybe give sqlite3 a go, that works fine for me with the same setup as yours.

    Are you using:

    …in your template?

  • 2
    Steven
    October 24th, 2006 21:05

    yes, I used the code in my template, and it work fine.

  • 3
    Brian Donovan
    December 13th, 2006 09:11

    I found that this is more generically applicable:


    def set_charset
    content_type = headers["Content-Type"] || 'text/html'
    if /^text\//.match(content_type)
    headers["Content-Type"] = "#{content_type}; charset=utf-8"
    end
    true
    end

Leave a Reply

You must be logged in to post a comment.