Ruby on Rails practices-Chinese Garbled text

Source: Internet
Author: User
Tags ruby on rails

Chinese garbled characters:

1. The. html. ERB file in the viewsdirectory directly writes Chinese characters and displays garbled characters in the browser.

2) The Chinese characters saved to the MySQL database are displayed normally on the database and interface, but garbled characters are output in the controller.

 

Solution:

Use the iconv Library

See http://www.kuqin.com/rubycndocument/man/addlib/Iconv.html

 

Interface Chinese display garbled solution (html. ERB in the Chinese gb2312 encoding, the output needs to be converted to UTF-8 ):

Set

<P> Hello </P>

Change

<P>

<% = Iconv. Conv ("UTF-8", "gb2312", "hello") %>

</P>

 

The Controller outputs the Chinese language extracted from the database (the database is saved as a UTF-8 Code) to the console Chinese for garbled solution:

Add the following code:

Require "iconv"

Use Time

@ Products = product. All

@ Products. Each do | T |

Iconv. Conv ("gb2312", "UTF-8", T. Title)

End

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.