Encoding :: UndefinedConversionError: "\ xC2" from ASCII-8BIT to UTF-8

When saving a new contact, I received the following error. Is there a way to make "\xC2" in such a way that it can be forcibly saved in UTF-8 format?

 c = Contact.new c.save! 

Encoding :: UndefinedConversionError: "\ xC2" from ASCII-8BIT to UTF-8: INSERT INTO "contacts" ("body", "created_at", "email", "updated_at") VALUES (?,?,?,?)

+6
source share
2 answers

Your string is in some other encoding, most likely iso-8859-1, so you have to run it to convert it:

 "\xC2".encode("iso-8859-1").force_encoding("utf-8") => "Γƒ" 

See this question for more information related to this issue.

+17
source

For what it's worth, I had this problem when I read in a code file in which the degree symbol (Β°) was marked. After json coding, ruby ​​became incredibly miserable.

What threw me into the loop was that there was no β€œΓƒβ€ in the code, so this just needs to be kept in mind.

+2
source

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


All Articles