我们仍然在depot目录上的命令行进行操作。
输入>ruby script/generate scaffold product title:string description:text image_url:string
D:\rails\depot>ruby script/generate scaffold product title:string description:text i
mage_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/20081029072221_create_products.rb
D:\rails\depot>
这一句命令包含了相当丰富的内容。 顾名思义,scaffold,暂且我们将之意思归到“架构”。我们通过这句话建立了一个这样的架构,它有自己的页面,可以通过http://localhost:3000/products访问,它包涵了一个叫products的表,这个表的内容就显示在刚刚的页面中。并且这个表有3列,第一列是数据类型为string的title,然后是数据类型为text的description,然后是string的image_url。
输入>rake db:migrate 将其迁移到数据库。
(in D:/rails/depot)
== 20081029072221 CreateProducts: migrating ===================================
-- create_table(:products)
-> 0.0620s
== 20081029072221 CreateProducts: migrated (0.0620s) ==========================
D:\rails\depot>
>ruby script/server
浏览http://localhost:3000/products
我们拥有了属于我们自己的第一个页面。虽然它很简陋,但是它的功能确实及其强大的。我们只输入了一个命令,便拥有了一个可以存储,修改,删除,添加数据(书籍)的页面。



到现在我们还未修改过任何源码,让我们先去解决一个小小的问题。很多人不想看到rails的默认页面,我们可以修改一些源文件让http://localhost:3000指向products页面。
找到public下面的index.html,删除之。
在depot/config/routes.rb中找到如下两行。
# You can have the root of your site routed with map.root -- just remember to delete public/index.html.
# map.root :controller => "welcome"
将其更改为:
# You can have the root of your site routed with map.root -- just remember to delete public/index.html.
map.root :controller => "products"
重启server,输入http://localhost:3000,我们便能看到熟悉的products页面了。
浙公网安备 33010602011771号