How to fix / convert an invalid string value?

I recently ran into this problem:

Mysql2::Error: Incorrect string value: '\xCC\x81T LO...' for column

I found many similar topics here on SO, but most of them are related to MySQL. Is there a universal way (method) to automatically remove "bad" / wrong characters from an input string so that I can save them in MySQL?

Thanks in advance.

+4
source share
4 answers

If this is a problem with characters that require 4 bytes, then you need to use

encoding: utf8mb4
collation: utf8mb4_bin

in database.yml

+3
source

Ensure that the same encoding / encoding set is specified in the column, database, and application.

  • MySQL: http://dev.mysql.com/doc/refman/5.7/en/charset-column.html
  • ActiveRecord: ActiveRecord::Base.connection.collation
  • Rails config.encoding sets up the application-wide encoding. Defaults to UTF-8.

str.force_encoding("UTF-8")

+1

. database.yml

encoding: utf8

, .

0

RoR: application_controller.rb

  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
0

Source: https://habr.com/ru/post/1649517/


All Articles