`
jsntghf
  • 浏览: 2468426 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

中文乱码问题

阅读更多

1、确定MySQL数据库编码是utf8
2、database.yml里面增加encoding: utf8
3、确定rhtml文件编码是UTF-8

 

1、确定MySQL数据库编码是utf8

 

ALTER TABLE tbl_name CONVERT TO CHARACTER SET  utf8; 

 

也可以通过migrate指定:

 

class CreateUsers < ActiveRecord::Migration
  def self.up
     create_table :users, :options => 'CHARSET=utf8' do |t|   
       t.column :name, :string
       t.column :password, :string
       t.column :birthday, :datetime
       t.column :email, :string
       t.column :address, :text
    end
  end

  def self.down
     drop_table :users
  end
end

 

2、database.yml里面增加encoding: utf8

 

development:
adapter: mysql
encoding: utf8
database: test
username: root
password: 
host: localhost

 

3、确定rhtml文件编码是UTF-8

 

  class ApplicationController < ActionController::Base

    before_filter :set_charset
    before_filter :configure_charsets

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

    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

 

在网页中,添加meta标签:

 

<meta http-equiv="content-type" content="text/html;charset=UTF-8" />

 

分享到:
评论
2 楼 RunUpwind 2011-04-13  
我现在使用 ruby1.92 + rails3.05 遇到这个问题:incompatible character encodings: UTF-8 and ASCII-8BIT, 用以上方法试了,没能解决。
1 楼 fansofjava 2010-08-23  
很好,很强大

相关推荐

Global site tag (gtag.js) - Google Analytics