Recently, I fell in love with ror and started to use it to do some small things, but there was always a garbled problem. Finally, I found a lot of ideas to solve Chinese Garbled text and shared them with you.
Development Environment: redrails use code: UTF-8 Database: MySQL
-
- In radrails, in the project's Property dialog box, select the info node on the left and set the encoding method to UTF-8 on the right
-
- Modify MySQL configuration file c: \ Program Files \ mysql \ MySQL Server 5.0 \ My. INI, change the default-character-set = utf8 in two of them, and then restart MySQL (Windows Service ). This step can also be done using instance wizard.
-
- Modify applicationcontroller as follows:
Class applicationcontroller <actioncontroller: Base
Before_filter: configure_charsets
Def configure_charsets
# @ Headers ["Content-Type"] = "text/html; charset = UTF-8"
@ Response. headers ["Content-Type"] = "text/html; charset = UTF-8"
# Set connection charset. MySQL 4.0 doesn ?? T support this so it
# Will throw an error, MySQL 4.1 needs this
Suppress (activerecord: statementinvalid) Do
Activerecord: base.connection.exe cute 'set names utf8'
End
End
End
-
- Add:
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
- Modify config/database. yml as follows:
Development:
Adapter: MySQL
Database: depot_development
Encoding: utf8 * Note space *
Username: Root
Password:
HOST: localhost
After saving, restart Ror, start the app, and run Ruby script/generate scaffold product admin again. The tables in the database should all be utf8_general_ci, and rhtml should also be UTF-8 encoded. Add the <meta> tag to \ app \ views \ layouts \ ADMIN. rhtml.
I used it to solve my Chinese Garbled text problem. Let's take a look at it and hope it will be as good as me !!!