今天按照Agile Web Development with Rails 的depot例子,準(zhǔn)備根據(jù)數(shù)據(jù)庫(kù)表自動(dòng)生成scaffold,
那天在公司把這個(gè)例子做了一遍,覺(jué)得挺順利,沒(méi)想到今天換到家里的機(jī)子后,出現(xiàn)了不少問(wèn)題.
在這里記錄一下出現(xiàn)的問(wèn)題及解決辦法.
開(kāi)發(fā)環(huán)境基本就是
WinXP
ruby1.8.6
rails1.2.3
MySQL Community Edition5.0.37
1.創(chuàng)建好數(shù)據(jù)庫(kù)和products表后,執(zhí)行
cmd>ruby script/generate scaffold product
結(jié)果報(bào)錯(cuò)
exists app/controllers/


error Before updating scaffolding from new DB schema, try creating a ta
le for your model (Product)
在google上搜了一下,大概有2種解決辦法:
(1) 在database.yml文件中,在密碼項(xiàng)前面加個(gè)空格(原文地址
http://www.javaeye.com/post/264530)
(2) 數(shù)據(jù)表名稱沒(méi)有復(fù)數(shù),默認(rèn)情況下,如果表為products,則命令相應(yīng)為....scaffold product
這個(gè)規(guī)則可以在相應(yīng)的配置文件中修改,具體可以google,這里不多講了.
這兩種方法都試過(guò)了,但問(wèn)題依舊.
(3)無(wú)意中打開(kāi)database.yml文件,發(fā)現(xiàn)有這樣幾句提示:
# Install the MySQL driver:
# gem install mysql
# On MacOS X:
# gem install mysql -- --include=/usr/local/lib
# On Windows:
# gem install mysql
# Choose the win32 build.
# Install MySQL and put its /bin directory on your path.
于是執(zhí)行了gem install mysql,之后問(wèn)題就解決了.奇怪的是我在辦公室一樣的環(huán)境,沒(méi)有執(zhí)行g(shù)em install mysql,也沒(méi)有出現(xiàn)過(guò)該問(wèn)題.唯一的區(qū)別,呵呵,大概就是家里網(wǎng)速慢,那天gem intall rails時(shí)
中斷了好幾次,不過(guò)這個(gè)問(wèn)題總算解決了..
2. 連接數(shù)據(jù)庫(kù)編碼問(wèn)題
生成scaffold后,就可以CRUD了,但是發(fā)現(xiàn)執(zhí)行完CRUD后,mysql數(shù)據(jù)庫(kù)中記錄為亂碼,執(zhí)行
mysql>set names gbk后,還是顯示亂碼,depot數(shù)據(jù)庫(kù)、products表均已設(shè)置為utf8格式,在
database.yml中加入
encoding:utf8后,問(wèn)題解決.
development:
adapter: mysql
database: depot
username: root
password:
host: localhost
encoding: utf8