RubyPDF Blog English,Ruby on Rails,Ubuntu UTF-8,Ruby on Rails under Ubuntu

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

My environment:
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
IE: 6.0

3 thoughts on “UTF-8,Ruby on Rails under Ubuntu”

  1. 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. 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"

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.