上文回顧:上文說明了在windows中快速創建環境的方法。
教你10分鍾內在Windows上完成Rails開發環境的安裝和配置
本文內容:本文將介紹,那些效率非常高的快速創建文件和數據庫的命令,包括:
- 使用mysql快速創建項目;
- 一個命令創建項目的開發和測試數據庫;
- 用命令創建數據庫中的表的流程;
- 一個命令創建控制器和視圖;
- 一個命令創建模型和數據庫文件;
- 一個命令創建全部文件和數據庫;
- 取消之前的創建操作的命令;
1,新建項目
先切換到准備放置項目的目錄:
cd rudy/work
然后,創建新的項目,我使用mysql 數據庫,所以加了--database=mysql參數,如果不加參數,rails的默認數據庫是SQLite3 :
提示:經過多次測試,網絡上其它兩種方法-d mysql 和-database=mysql都不會自動生成database.yml文件為mysql的配置。
rails new jiaocheng --database=mysql
創建完成后,使用dos的dir /p/w查看創建了哪些文件和目錄
2,創建數據庫:
數據庫的配置信息在config/database.yml文件中。
development: adapter: mysql2 encoding: utf8 reconnect: false database: jiaocheng_development pool: 5 username: root password: host: localhost test: adapter: mysql2 encoding: utf8 reconnect: false database: jiaocheng_test pool: 5 username: root password: host: localhost production: adapter: mysql2 encoding: utf8 reconnect: false database: jiaocheng_production pool: 5 username: root password: host: localhost
配置分別對應開發、測試和生成數據庫的配置,把他們修改為自己的配置。
創建數據,如果mysql運行正常,rails配置正確,使用rake db:create命令,會在msyql中創建jiaocheng_test和jiaocheng_development兩個數據庫。命令如下,
rake db:create
3,創建后台控制器:
首先,刪除public/index.html文件,因為rails優先讀取public目錄下的靜態文件。
然后,創建一個名字叫User的控制器,控制中有一個home方法。
(注意:區分大小寫。http://localhost:3000/user/home可以訪問,user/Home是不能訪問的。)命令如下,
rails generate controller User home
最后,設置首頁。找到config/route.rb中,root :to=>'User#home',去掉注釋並設置為自己的控制器。如圖,
這時再訪問:http://localhost:3000,結果如下,
4,增加或修改數據庫中表的流程是:
首先,創建一個migrate文件,
rails generate migrate add_price_to_product price:decimal
然后,調整文件,增加數據限制等信息,文件內容如下:
(提示:rails 3.1添加了一個change 更智能的方法,它可以判斷何時回滾,就不再單獨書寫down方法。如果你在其它資料里看到up配合down的方法,仍然可以使用)
class AddPriceToProduct < ActiveRecord::Migration def change add_column :products, :price, :decimal end end
最后,使用rake db:migrate方法。
rake db:migrate
除了add_column 此文件中支持的方法還有:
create_table change_table drop_table add_column change_column rename_column remove_column add_index remove_index
5,同時創建模型和數據庫:
rails 的Migrations 還提供了同時創建模型和數據庫中的表的命令:
rails generate model Product name:string description:text
這會創建模型和一個db/migrate/*.rb的文件,適當修改此文件,然后執行,
rake db:migrate
查看數據庫,發現增加了表products。注意:表名稱比輸入的名稱多了"s",這是自動加的,用於區分英文的單數和復數。如果使用拼音,也會自動添加一個"s"。
6,使用腳手架快速建立一組Model,、Views、Controller和數據庫:
使用scaffold,
rails generate scaffold Post name:string title:string content:text
產生的15 個文件錄,以下是個簡單的說明:
檔案 | 目的 |
---|---|
db/migrate/20120104214725_create_posts.rb | 用來建立posts 資料庫資料表的Migration (你的檔案開頭名稱會有不同的timestamp) |
app/models/post.rb | Post model |
test/fixtures/posts.yml | 用來測試的假文章資料 |
app/controllers/posts_controller.rb | Posts controller |
app/views/posts/index.html.erb | 用來顯示所有文章的index 頁面 |
app/views/posts/edit.html.erb | 用來編輯文章的頁面 |
app/views/posts/show.html.erb | 用來顯示特定一篇文章的頁面 |
app/views/posts/new.html.erb | 用來新增文章的頁面 |
app/views/posts/_form.html.erb | 用來顯示編輯和新增文章的表單partial |
app/helpers/posts_helper.rb | 可在文章views 中使用的Helper 函式 |
test/unit/post_test.rb | posts model 的單元測試 |
test/functional/posts_controller_test.rb | posts controller 的功能測試 |
test/unit/helpers/posts_helper_test.rb | posts helper 的單元測試 |
config/routes.rb | 設定URL路由規則的檔案 |
public/stylesheets/scaffold.css | CSS樣式檔案 |
調整db/migrate/*.rb文件,后執行rake db:migrate,在數據庫中添加Posts表。
rake db:migrate
7,回退操作:
如果你跟隨我上面的操作,就會創建了許多文件,例如創建了控制器,命令如下,
rails generate model Product
如何刪除和取消上面的操作呢?使用destroy命令,它的作用和generate相反。
rails destroy model Product
數據庫的操作如何取消?取消上一步,
rake db:rollback
取消前三部操作,
rake db:rollback STEP=3
我們知道db/migrate/*.rb文件是用時間戳命名的,如果你在其中寫了特定的:up :down,可以指定回退某一個版本,命令如下,
rake db:migrate:up VERSION=20120104120000