2010-07-23 134 views
0

我在閱讀「使用Rails進行實用敏捷Web開發(第2版)」,並嘗試從本書中獲取示例。但是當我正在嘗試其中一個示例時,我遇到了錯誤。爲模型創建視圖

所以:

  • 我創建了一個模型產品

    ruby script/generate model product 
    

    填充字段:

    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 
        end 
    end 
    
    def self.down 
        drop_table :products 
    end 
    end 
    

    生成的DB:

    rake db:migrate 
    
  • 接下來,我已經creaded的觀點:

    ruby script/generate controller admin 
    

    添加行的觀點:

    class AdminController < ApplicationController 
        scaffold :product 
    end 
    

我用Google搜索,發現這個解決方案:

ruby script/generate scaffold product title:string description:text image_url:string 

但我不知道這是一個正確的做法。爲產品表創建視圖的「真正方法」是什麼?

+0

腳手架發電機可以作爲未來發展的基礎,不僅景色也模型和控制器。它將創建特定模型的所有MVC結構。當你用rails做第一步時,這似乎是最好的解決方案。然後你會決定是否適合你 – fantactuka 2010-07-23 12:45:34

回答

2

scaffold自2.0版本以來,方法已從Rails中刪除。從那時起,應該使用腳手架發電機。

1

要創建一個例子你只運行

ruby script/generate scaffold Product title:string description:text image_url:string 

,這將產生MVC結構產品

+0

逗號不需要在這裏。看作者的工作解決方案。 – 2010-07-23 11:14:51

+0

更新,謝謝 – fantactuka 2010-07-23 11:30:53