这几天看《Agile Web Development With Rails, 2nd Edition》(AWDWR),体验一下rails开发的敏捷。
不料第一个例子就卡住了。原来书上的例子所需要使用的方法是在rails1.2中有效的,而在rails2.2不被默认支持了。


我遇到的这个东西就是那个scaffold!从网上搜索发现大家都说要装plugin才能够使用,可是找到plugin的老巢也没看出什么门道。
于是打开AWDWR,3rd edition beta电子书来看,才发现这个东西已经是可以直接作为script/generate的对象来使用了。


下面是成功的使用方式,创建一个product的scaffold:
---------------------------------------------
depot> ruby script/generate scaffold product \
> title:string description:text image_url:string
exists app/models/
exists app/controllers/
exists app/helpers/
create app/views/products
exists app/views/layouts/
exists test/functional/
exists test/unit/
exists public/stylesheets/
create app/views/products/index.html.erb
create app/views/products/show.html.erb
create app/views/products/new.html.erb
create app/views/products/edit.html.erb
create app/views/layouts/products.html.erb
create public/stylesheets/scaffold.css
create app/controllers/products_controller.rb
create test/functional/products_controller_test.rb
create app/helpers/products_helper.rb
route map.resources :products
dependency model
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/product.rb
create test/unit/product_test.rb
create test/fixtures/products.yml
create db/migrate
create db/migrate/20080601000001_create_products.rb
-------------------------------------------------------
从上面的输出可以看出,这个命令已经整合起原来的命令方式。

下面是原来的方式:
------------------------
script/generate model product 并且修改db/migrate下的任务
script/generate controller admin  并且修改app/controllers/admin下的admin_controller.rb,增加scaffold :product
------------------------

下面的迁移任务还是同以前一样操作:
--------------------------------------------------------
depot> rake db:migrate
(in /Users/rubys/work/depot)
== 20080601000001 CreateProducts: migrating ===================================
-- create_table(:products)
-> 0.0027s
== 20080601000001 CreateProducts: migrated (0.0028s) ==========================
----------------------------------------------------

可以查看数据的版本:
----------------------------------------------------
depot> sqlite3 db/development.sqlite3 "select version from schema_migrations"
20080601000001
---------------------------------------------------

启动服务器:
---------------------------------------------------
depot> ruby script/server
=> Booting WEBrick...
=> Rails application started on http://0.0.0.0:3000
=> Ctrl-C to shutdown server; call with --help for options
[2008-03-27 11:54:55] INFO WEBrick 1.3.1
[2008-03-27 11:54:55] INFO ruby 1.8.6 (2007-09-24) [i486-linux]
[2008-03-27 11:54:55] INFO WEBrick::HTTPServer#start: pid=6200 port=3000
----------------------------------------------------

对比下两个版本中操作的便捷程度:
rails 1.2中, 输入命令 5 次,修改文件 2 处,
rails 2.2中,输入命令 3 次,修改文件 0 处。
可以明显看出新版的效率有了如此大的改进,真是让人欣喜。现在Rails仍然处于一个高速发展的阶段,相信不久它很快还能为我们带来更加便捷的开发方式。

--
Liu Lantao
College of Information Science and Technology, Beijing Normal University
EMAIL: liulantao ( at ) gmail ( dot ) com ;
WEBSITE: http://www.liulantao.com/ .
------