How to convert text to unicode in Rails?

In my database, I have the following entry

id     |      name      |      info
1          John Smith         Çö ¿¬¼

As you can tell, the info column is displayed incorrectly - in fact it is Korean. In Chrome, when I switch the browser encoding from UTF-8 to Korean ("euc-kr", I think), I really manage to view the text as such:

id     |      name      |      info
1          John Smith        횉철 쩔짭쩌

Then I manually copy the text to the information in the database and save, and now I can view it in UTF-8 without switching the encoding of the browser.

Tall. Now I would like to get the same in Rails, and not manually. Therefore, starting from the initial recording, I go to the console and print:

require 'iconv'
u = User.find(1)
info = u.info
new_info = Iconv.iconv('euc-kr','UTF-8', info)
u.update_attribute('info', new_info)

However, in the end I get something like \x{A2AF}\x{A8FA}\x{A1C6} \x{A2A5}\x{A8A2}in the database, not 횉철 쩔짭쩌.

I have a very general understanding of Unicode and coding.

- , ? - , .

!

+3
2

. . , , , , .

:

1:

. euc-kr utf-8, , :

Iconv.iconv('UTF-8', 'euc-kr', info)

2:

- , , , Iconv :

Iconv.iconv('UTF-8//IGNORE', 'euc-kr', info)

, REAL KOREAN TEXT, yay! , , - :

UPDATE `users` SET `info` = '--- \n- \"\\xEC\\xB2\\xA0\\xEC\\xB1\\x8C...' etc...

, . ? .

3:

Iconv - . , join:

Iconv.iconv('UTF-8//IGNORE', 'euc-kr', info).join

!

:

require 'iconv'
u = User.find(1)
info = u.info
new_info = Iconv.iconv('UTF-8//IGNORE','euc-kr', info).join
u.update_attribute('info', new_info)

, , (, , , ).

+4

Iconv ? -, , , - utf8 script, , Iconv

0

All Articles