# About chinese display

**URL:** <https://www.ruby-forum.com/t/about-chinese-display/196428>\
**Category:** Rails\
**Created:** [August 12, 2007, 6:32pm UTC](https://www.ruby-forum.com/t/about-chinese-display/196428 "2007-08-12T18:32:45Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [August 12, 2007, 6:32pm UTC](https://www.ruby-forum.com/t/about-chinese-display/196428/1 "2007-08-12T18:32:45Z")

</div>

hello:  
I have built a project with Instant Rails.Now only a database and a  
scaffold based on a product model have finished.But when i inputted  
“…/admin/list” in my browser,i found my chinese characters cound not  
display normally.Then i made five steps:  
1 ajusting encoding in my browser menu  
2 in “C:\InstantRails\conf\_files\my.ini"directory,making sure  
“my.ini” file containg “[mysqld] default-character-set=utf8  
[client] default-character-set=utf8” setting.  
3 adding “encoding: utf8” in my “config/database.yml” file  
4 making all the “.rtml"and”.rb” files saved in “utf8” way  
5 deleting my database and rebuilding the database  
When i made my “three” migration using “rake db:migrate”,an error  
displayed in my console window saying “001\_create\_products.rb:14:syntax  
error, unexpected kEND,expecting $end”.  
How do i manage this problem?  
More important,i want to know how can i make my app to display  
chinese correctly.  
thank you!

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [August 13, 2007, 12:22am UTC](https://www.ruby-forum.com/t/about-chinese-display/196428/2 "2007-08-13T00:22:43Z")

</div>

Guo Y. wrote:

> ```
> 4 making all the ".rtml"and".rb" files saved in "utf8" way
> 5 deleting my database and rebuilding the database
> When i made my "three" migration using "rake db:migrate",an error
> 
> ```
> 
> displayed in my console window saying “001\_create\_products.rb:14:syntax  
> error, unexpected kEND,expecting $end”.  
> How do i manage this problem?  
> More important,i want to know how can i make my app to display  
> chinese correctly.  
> thank you!

Like the error says you have a syntax error in your  
001\_create\_products.rb migration. If you want us to help you find it  
you’ll need to show it to us.

As for displaying Chinese characters you are almost there. You’ll  
probably need something like this in the section of your pages:

Other variations on the above described here:

> **[Character encodings](https://www.w3.org/International/O-charset)**
>
> How to declare the character encoding of a document in XML or HTML, and various useful links to related information

–  
Michael W.

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [August 13, 2007, 3:38am UTC](https://www.ruby-forum.com/t/about-chinese-display/196428/3 "2007-08-13T03:38:36Z")

</div>

Ok,the following are all the codes in the 001\_create\_products.rb.

class CreateProducts \< ActiveRecord::Migration  
def self.up  
create\_table :products do |t|  
t.column(:title, :string)  
t.column(:description,:text)  
t.column(:image\_url,:string)  
t.column(:stock,:integer)  
end  
end

def self.down  
drop\_table :products  
end  
end

Michael W. wrote:

> Guo Y. wrote:
> 
> > ```
> > 4 making all the ".rtml"and".rb" files saved in "utf8" way
> > 5 deleting my database and rebuilding the database
> > When i made my "three" migration using "rake db:migrate",an error
> > 
> > ```
> > 
> > displayed in my console window saying “001\_create\_products.rb:14:syntax  
> > error, unexpected kEND,expecting $end”.  
> > How do i manage this problem?  
> > More important,i want to know how can i make my app to display  
> > chinese correctly.  
> > thank you!
> 
> Like the error says you have a syntax error in your  
> 001\_create\_products.rb migration. If you want us to help you find it  
> you’ll need to show it to us.
> 
> As for displaying Chinese characters you are almost there. You’ll  
> probably need something like this in the section of your pages:
> 
> Other variations on the above described here:
> 
> [Character encodings](http://www.w3.org/International/O-charset)
> 
> –  
> Michael W.

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [August 13, 2007, 4:37am UTC](https://www.ruby-forum.com/t/about-chinese-display/196428/4 "2007-08-13T04:37:59Z")

</div>

Guo Y. wrote:

> end
> 
> def self.down  
> drop\_table :products  
> end  
> end

That looks okay. It ran on my setup, copy and pasting from above.

I would suggest typing it over in a new file that you create with  
“script/generate migration”. If you are running a double-byte or other  
non-Latin input scheme there may be something odd embedded in the file.

–  
Michael W.

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [August 13, 2007, 5:16am UTC](https://www.ruby-forum.com/t/about-chinese-display/196428/5 "2007-08-13T05:16:59Z")

</div>

Thank you!I have done it according yours advice.But do i need to modify  
the following to make chinese diplay?  
2 making sure"my.ini" file containg “[mysqld]  
default-character-set=utf8 [client] default-character-set=utf8” setting.  
3 adding “encoding: utf8” in my  
“config/database.yml” file

Michael W. wrote:

> Guo Y. wrote:
> 
> > end
> > 
> > def self.down  
> > drop\_table :products  
> > end  
> > end
> 
> That looks okay. It ran on my setup, copy and pasting from above.
> 
> I would suggest typing it over in a new file that you create with  
> “script/generate migration”. If you are running a double-byte or other  
> non-Latin input scheme there may be something odd embedded in the file.
> 
> –  
> Michael W.

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [August 13, 2007, 8:24am UTC](https://www.ruby-forum.com/t/about-chinese-display/196428/6 "2007-08-13T08:24:10Z")

</div>

Guo Y. wrote:

> Thank you!I have done it according yours advice.But do i need to modify  
> the following to make chinese diplay?  
> 2 making sure"my.ini" file containg “[mysqld]  
> default-character-set=utf8 [client] default-character-set=utf8” setting.  
> 3 adding “encoding: utf8” in my  
> “config/database.yml” file

You do need the “encoding: utf8” line in your database.yml file.

For MySQL there’s a bunch of different ways to set the database or table  
to use utf8. I don’t typically use Windows with MySQL so I’ve never  
configured a my.ini file but you should be able to confirm the character  
set within the mysql command line tool with “show create database  
\<database\_name\>”.

E.g. for my Rails app that can display utf8 characters I get:

±--------------------±----------------------------------------------------------------------------------------------+  
| Database | Create Database  
|  
±--------------------±----------------------------------------------------------------------------------------------+  
| topsites\_production | CREATE DATABASE `topsites_production` /\*!40100  
DEFAULT CHARACTER SET utf8 COLLATE utf8\_bin \*/ |  
±--------------------±----------------------------------------------------------------------------------------------+

–  
Michael W.

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [August 13, 2007, 10:14am UTC](https://www.ruby-forum.com/t/about-chinese-display/196428/7 "2007-08-13T10:14:26Z")

</div>

Thank you!When i use command “show create database  
\<database\_name\>”,the console window show that database’s coding is  
“latin1”.Later I added line "encoding: utf8"in my  
database.yml,remigrated my three migrations,then console window said:

# D:\ruby\cashmere\>rake db:migrate VERSION=0 (in D:/ruby/cashmere) == AddTestData: reverting

# == AddTestData: reverted (0.1400s)

# == AddPrice: reverting

# – remove\_column(:products, :price) -\> 0.1870s == AddPrice: reverted (0.1870s)

# == CreateProducts: reverting

# – drop\_table(:products) -\> 0.0470s == CreateProducts: reverted (0.0470s)

# D:\ruby\cashmere\>rake db:migrate (in D:/ruby/cashmere) == CreateProducts: migrating

# – create\_table(:products) -\> 0.0930s == CreateProducts: migrated (0.0930s)

# == AddPrice: migrating

# – add\_column(:products, :price, :integer, {:default=\>0}) -\> 0.1250s == AddPrice: migrated (0.1250s)

# == AddTestData: migrating

rake aborted!  
Mysql::Error: Illegal mix of collations (latin1\_swedish\_ci,IMPLICIT) and  
(utf8\_g  
eneral\_ci,COERCIBLE) for operation ‘=’: SELECT \* FROM products WHERE  
(products.t  
itle = ‘ç™½é¢†ç”·’) LIMIT 1

```
 This shows mysql setting is not right.How can i manage it?I am a

```

fresher to mysql.

Michael W. wrote:

> Guo Y. wrote:
> 
> > Thank you!I have done it according yours advice.But do i need to modify  
> > the following to make chinese diplay?  
> > 2 making sure"my.ini" file containg “[mysqld]  
> > default-character-set=utf8 [client] default-character-set=utf8” setting.  
> > 3 adding “encoding: utf8” in my  
> > “config/database.yml” file
> 
> You do need the “encoding: utf8” line in your database.yml file.
> 
> For MySQL there’s a bunch of different ways to set the database or table  
> to use utf8. I don’t typically use Windows with MySQL so I’ve never  
> configured a my.ini file but you should be able to confirm the character  
> set within the mysql command line tool with “show create database  
> \<database\_name\>”.
> 
> E.g. for my Rails app that can display utf8 characters I get:
> 
> ±--------------------±----------------------------------------------------------------------------------------------+  
> | Database | Create Database  
> |  
> ±--------------------±----------------------------------------------------------------------------------------------+  
> | topsites\_production | CREATE DATABASE `topsites_production` /\*!40100  
> DEFAULT CHARACTER SET utf8 COLLATE utf8\_bin \*/ |  
> ±--------------------±----------------------------------------------------------------------------------------------+
> 
> –  
> Michael W.

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [August 13, 2007, 10:22am UTC](https://www.ruby-forum.com/t/about-chinese-display/196428/8 "2007-08-13T10:22:20Z")

</div>

Guo Y. wrote:

> fresher to mysql.  
> I do it like this inside the MySQL command line tool:

create database cashmere\_development character set utf8 collate  
utf8\_bin;  
create database cashmere\_test character set utf8 collate utf8\_bin;  
create database cashmere\_production character set utf8 collate utf8\_bin;

–  
Michael W.

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [August 15, 2007, 2:30pm UTC](https://www.ruby-forum.com/t/about-chinese-display/196428/9 "2007-08-15T14:30:26Z")

</div>

Guo Y. wrote:

> ```
> def self.up
> 
> ```
> 
> end  
> Mysql::Error: Can’t create table ‘.\cashmere\_development\products.frm’  
> column?If so,how do i set it in rail’migration file like  
> 001\_create\_products.rb?  
> Thank you!

Something is messed up in your MySQL database. Google on “errno 121” for  
more info. Here’s an example of what might be the problem:

[http://www.jsw4.net/info/listserv\_archives/mysql/03-wk07/msg00159.html](http://www.jsw4.net/info/listserv_archives/mysql/03-wk07/msg00159.html)

–  
Michael W.

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [August 15, 2007, 1:54pm UTC](https://www.ruby-forum.com/t/about-chinese-display/196428/10 "2007-08-15T13:54:48Z")

</div>

Thank you!But i still have to bother you!  
I follow these steps to manage the chinese display.  
1: use mysql command “create database cashmere\_development default  
character set utf8 collate utf8\_bin;” to create my database that support  
utf8.This step succeeds.  
2: add “encoding: utf8” to the cash\_development section of the  
dababase.yml file.  
3: create model and migration using rails command"ruby script/generate  
model product",then modify the 001\_create\_products.rb file like this:  
class CreateProducts \< ActiveRecord::Migration  
def self.up  
create\_table :products do |t|  
t.column :title,:string  
t.column :description,:text  
t.column :image\_url,:string  
t.column :stock,:integer  
end  
end

def self.down  
drop\_table :products  
end  
end

at last,migrate it using"rake db:migrate".Here,i got an error saying:

# D:\ruby\cashmere\>rake db:migrate (in D:/ruby/cashmere) == CreateProducts: migrating

– create\_table(:products)  
rake aborted!  
Mysql::Error: Can’t create table ‘.\cashmere\_development\products.frm’  
(errno: 1  
21): CREATE TABLE products (`id` int(11) DEFAULT NULL auto\_increment  
PRIMARY KEY  
, `title` varchar(255) DEFAULT NULL, `description` text DEFAULT NULL,  
`image_url` varchar(255) DEFAULT NULL, `stock` int(11) DEFAULT NULL) ENGINE=InnoDB

```
How do i manage this problem? Is that because utf8 can not be used 

```

for “stock” column?Should i define diffrent character set for diffrent  
column?If so,how do i set it in rail’migration file like  
001\_create\_products.rb?  
Thank you!

Michael W. wrote:

> Guo Y. wrote:
> 
> > fresher to mysql.  
> > I do it like this inside the MySQL command line tool:
> 
> create database cashmere\_development character set utf8 collate  
> utf8\_bin;  
> create database cashmere\_test character set utf8 collate utf8\_bin;  
> create database cashmere\_production character set utf8 collate utf8\_bin;
> 
> –  
> Michael W.

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [August 15, 2007, 5:05pm UTC](https://www.ruby-forum.com/t/about-chinese-display/196428/11 "2007-08-15T17:05:27Z")

</div>

```
Thank you!I think i have solved this problem by dropping database in 

```

command line instead of deleting it manually.My page now can display  
chinese,but i found a unneccesary line at top of my page  
saying:ï»„1?7!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”  
“[http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd](http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd)”\> .Although  
this is not an important flaw,I don’t know why?  
It is harmful to my page appearance.  
Thank you !

Michael W. wrote:

> Guo Y. wrote:
> 
> > ```
> > def self.up
> > 
> > ```
> > 
> > end  
> > Mysql::Error: Can’t create table ‘.\cashmere\_development\products.frm’  
> > column?If so,how do i set it in rail’migration file like  
> > 001\_create\_products.rb?  
> > Thank you!
> 
> Something is messed up in your MySQL database. Google on “errno 121” for  
> more info. Here’s an example of what might be the problem:
> 
> [http://www.jsw4.net/info/listserv\_archives/mysql/03-wk07/msg00159.html](http://www.jsw4.net/info/listserv_archives/mysql/03-wk07/msg00159.html)
> 
> –  
> Michael W.
