Guo Yangguang wrote:
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!
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 <head> section of your pages:
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
Other variations on the above described here:
Guo Yangguang wrote:
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
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.
Guo Yangguang 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:
Guo Yangguang wrote:
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.
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;
Guo Yangguang wrote:
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!
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