ruby on rails 显示乱码
最终解决方案
1、确定MySQL数据库编码是utf8
2、database.yml里面增加encoding: utf8
3、确定rhtml文件编码是UTF-8
4、修改ApplicationController(该文件位于:../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
5、针对表加UTF-8编码(前提是你要使用数据迁移,位于../db/migrate/xxx.rb)
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
最终解决方案
1、确定MySQL数据库编码是utf8
2、database.yml里面增加encoding: utf8
3、确定rhtml文件编码是UTF-8
4、修改ApplicationController(该文件位于:../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
5、针对表加UTF-8编码(前提是你要使用数据迁移,位于../db/migrate/xxx.rb)
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