Ruby on Rails Incorrect string value error resolution when posting a form in Japanese

Introduction

environment

error contents

Incorrect string value error occurs when I enter Japanese in the new post form and submit it. スクリーンショット 2021-01-04 21.28.20.png

Cause

From the error statement of Incorrect string value, it is inferred that the cause is the character string entered in the form. I entered English as a trial and sent it, and I was able to post without problems.

Does that mean that the DB does not support Japanese?

Connect to MySQL and check the settings of the database in use.

mysql> show variables like '%char%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | latin1                     |
| character_set_connection | latin1                     |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | latin1                     |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

The DB character code setting was latin1 instead of utf8. It seems that latin1 does not support Japanese in Western European languages.

The cause was that the character code setting in the database.yml file was missing when building the Docker environment.

database.yml


default: &default
  adapter: mysql2
  #Here "charset:The cause was that "utf8" was missing
  encoding: utf8
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password: hideki1104
  host: db

Solutions

in the etc/mysql/my.cnf file

[mysqld]
character-set-server=utf8mb4
collation-server=utf8mb4_bin
skip-character-set-client-handshake
[mysqldump]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4

You can change the character code by writing.

I tried to add a description using the vi editor, but the vi command is not set and cannot be used.

# vi my.cnf
/bin/sh: 5: vi: not found

It seems that you can install vim using apt_get.

# apt-get -v
apt 1.8.2.2 (amd64)

Update once with the following command

# apt-get update

Install vim.

# apt-get install vim

You can now use vim.

etc/mysql/my.cnf

[mysqld]
character-set-server=utf8mb4
collation-server=utf8mb4_bin
skip-character-set-client-handshake
[mysqldump]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4

I was able to change the character code by writing.

In this state, the character cards of DB, table, and column that have already been created have not been changed.

I typed the following command in MySQL DB character code setting

ALTER DATABASE DB name CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

Table character code setting

ALTER TABLE table name CONVERT TO character SET utf8mb4 COLLATE utf8mb4_unicode_ci;

Column character code setting

ALTER TABLE table name CHANGE column_name Column name VARCHAR(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

You can now submit Japanese using the post form.

Reference article

-Change MySQL VARIABLES from command -[When the vi command cannot be used in the docker container] (https://blog.azimicat.com/entry/2019/09/12/docker%E3%82%B3%E3%83%B3%E3%83%86%E3%83%8A%E3%81%AE%E4%B8%AD%E3%81%A7vi%E3%82%B3%E3%83%9E%E3%83%B3%E3%83%89%E3%81%8C%E4%BD%BF%E3%81%88%E3%81%AA%E3%81%84%E3%81%A8%E3%81%8D) -MySQL cannot change character code to UTF-8

Recommended Posts

Ruby on Rails Incorrect string value error resolution when posting a form in Japanese
Rails console Incorrect string value error handling
(Ruby on Rails6) Creating data in a table
[Ruby on Rails] How to write enum in Japanese
[Ruby on Rails Tutorial] Error in the test in Chapter 3
[Ruby / Rails] Set a unique (unique) value in the class
[Note] Error message when using Rails Form object pattern Japanese
Difficulties in building a Ruby on Rails environment (Windows 10) (SQLite3)
[Ruby on Rails] undefined method ʻid'for nil: NilClass error resolution method
From Ruby on Rails error message display to Japanese localization
How to display a graph in Ruby on Rails (LazyHighChart)
Apply CSS to a specific View in Ruby on Rails
[Rails] Japanese localization of error message when using Form object
Ruby on Rails Japanese-English support i18n
I get a Ruby version error when I try to start Rails.
(Ruby on Rails6) Creating a database and displaying it in a view
Implement a contact form in Rails
Change from SQLite3 to PostgreSQL in a new Ruby on Rails project
How to create a query using variables in GraphQL [Using Ruby on Rails]
"" Application.js "is not present in the asset pipeline" error in Ruby on Rails
What to do when Blocked Host: "host name" appears in Ruby on Rails
A series of flow of table creation → record creation, deletion → table deletion in Ruby on Rails
[Ruby on Rails] Follow function undefined method ʻid'for nil: NilClass error resolution
[Ruby on Rails] Creating an inquiry form
Ruby on Rails in Visual Studio Codespaces
[Ruby on Rails] Japanese notation of errors
Dealing with Mysql2 :: Error: Incorrect string value error
Beginners create portfolio in Ruby on Rails
[Ruby on Rails] Solving the addiction when setting crontab using whenever in EC2
How to implement image posting function using Active Storage in Ruby on Rails
[Ruby On Rails] Error in test using RSpec MySQL client is not connected
Error 400 occurs when executing Query by specifying Japanese as a condition in SOQL
[Ruby on Rails] How to Japaneseize the error message of Form object (ActiveModel)
Convert to a tag to URL string in Rails
Escape processing when creating a URL in Ruby
[Ruby on Rails] How to display error messages
[Rails] Error resolution when generating tokens with PAYJP
Error in bundle install when running rails new
[Ruby on Rails] NoMethodError undefined method `devise_for'error resolution
When the Ruby on Rails terminal rolls back
Recommendation of Service class in Ruby on Rails
Rails new in Ruby on Rails ~ Memorandum until deployment 2
[Rails] Solution when migration error occurs in acts-as-taggable-on
Introducing Rspec, a Ruby on Rails test framework
[Ruby on Rails] A memorandum of layout templates
Rails new in Ruby on Rails ~ Memorandum until deployment 1
[Ruby on Rails] How to install Bootstrap in Rails
[Ruby on Rails] Ranking display (total, average value)
[Ruby on Rails] Individual display of error messages
I made a portfolio with Ruby On Rails
[Ruby on Rails] I get a warning when running RSpec because gem'chromedriver-helper' is deprecated.
I have a question. I get an error when playing a video in Listview on android.
Count the number of occurrences of a string in Ruby
[Ruby on Rails] Asynchronous communication of posting function, ajax
[Introduction] Try to create a Ruby on Rails application
Rails uninitialized constant A simple checklist for error resolution
One case of solving a migration error in Rails
Build a Ruby on Rails development environment on AWS Cloud9
[Rails] About error resolution when installing devise and activeadmin
Error encountered with notes when deploying docker on rails
[Rails] "private method` String' called ~ "error when db: migrate