Skip to content

Instantly share code, notes, and snippets.

@qianjigui
Created August 22, 2014 03:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qianjigui/00502e6218a32526187f to your computer and use it in GitHub Desktop.
Save qianjigui/00502e6218a32526187f to your computer and use it in GitHub Desktop.
Rails MYSQL UTF8 Connection
Subject: [PATCH] Fix DB linking encoding to utf8
---
Gemfile | 2 +-
Gemfile.lock | 4 ++--
config/application.rb | 2 ++
config/database.yml | 12 ++++++------
config/environment.rb | 2 ++
5 files changed, 13 insertions(+), 9 deletions(-)
diff --git a/Gemfile b/Gemfile
index c218f03..8356b2f 100644
--- a/Gemfile
+++ b/Gemfile
@@ -6,7 +6,7 @@ gem 'rails', '4.0.0'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
-gem 'mysql'
+gem 'mysql2'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0'
diff --git a/Gemfile.lock b/Gemfile.lock
index e119aae..91c5629 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -57,7 +57,7 @@ GEM
mime-types (1.25.1)
minitest (4.7.5)
multi_json (1.10.1)
- mysql (2.9.1)
+ mysql2 (0.3.16)
polyglot (0.3.5)
rack (1.5.2)
rack-protection (1.5.3)
@@ -154,7 +154,7 @@ DEPENDENCIES
db_fixtures_dump
jbuilder (~> 1.2)
jquery-rails
- mysql
+ mysql2
rails (= 4.0.0)
rspec-rails
sass-rails (~> 4.0.0)
diff --git a/config/application.rb b/config/application.rb
index 9cce2b7..5d0815e 100644
--- a/config/application.rb
+++ b/config/application.rb
@@ -20,6 +20,8 @@ module PackageCMP
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
config.i18n.default_locale = :zh
+ config.encoding = 'utf-8'
+
config.action_dispatch.default_headers = {
'X-Frame-Options' => 'ALLOWALL'
}
diff --git a/config/database.yml b/config/database.yml
index be29147..6382f93 100644
--- a/config/database.yml
+++ b/config/database.yml
@@ -4,8 +4,8 @@
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
development:
- adapter: mysql
- encoding: utf8
+ adapter: mysql2
+# encoding: utf8
database: devdb
username: user
password: passwd
@@ -18,8 +18,8 @@ development:
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
- adapter: mysql
- encoding: utf8
+ adapter: mysql2
+# encoding: utf8
database: testdb
username: usert
password: passwd
@@ -29,8 +29,8 @@ test:
timeout: 5000
production:
- adapter: mysql
- encoding: utf8
+ adapter: mysql2
+# encoding: utf8
database: pdb
username: userp
password: passwd
diff --git a/config/environment.rb b/config/environment.rb
index b88dc42..fedfaa6 100644
--- a/config/environment.rb
+++ b/config/environment.rb
@@ -3,3 +3,5 @@ require File.expand_path('../application', __FILE__)
# Initialize the Rails application.
AAABBBCCC::Application.initialize!
+Encoding.default_external = Encoding::UTF_8
+Encoding.default_internal = Encoding::UTF_8
--
2.0.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment