2017 年 Ruby on Rails 5 解决 Emoji 报错问题

报错信息:

emoji

解决方法:

方法来自:http://jasonseifer.com/2014/06/20/rails-4-mysql-and-emoji-mysql2error-incorrect-string-value-error

我试了管用。

第一步:

config/database.yml 里加一条

collation: utf8mb4_unicode_ci

结果:

default: &default
  adapter: mysql2
  encoding: utf8
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password:
  encoding: utf8mb4
  charset: utf8mb4
  collation: utf8mb4_unicode_ci  # <-----------

development:
  <<: *default
  database: apartment_production

第 2 步:

去 MySQL 里跑:

ALTER TABLE `表名`
  CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_bin,
MODIFY '字段名' VARCHAR(250)
    CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;

我跑了2条,改了2个字段

ALTER TABLE `users`
  CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_bin,
MODIFY wechat_nickname VARCHAR(250)
    CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;

ALTER TABLE `users`
  CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_bin,
MODIFY username VARCHAR(250)
    CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;

问题解决:

emoji2

2017 年 Ruby on Rails 5 解决 Emoji 报错问题
Share this

Subscribe to 1c7 的博客