12 months ago

rails generate 是rails用来生成模板文件的命令,可以减少很多樣板代碼的输入工作.

常用的命令

rails g model
rails g controller
rails g scaffold
rails g migration

rails g model用法

rails generate model NAME [field[:type][:index] field[:type][:index]] [options]

rails g controller用法

rails generate controller NAME [action action] [options]

rails generate scaffold用法

rails generate scaffold NAME [field[:type][:index] field[:type][:index]] [options]

rails generate migration用法

rails generate migration NAME [field[:type][:index] field[:type][:index]] [options]

區别

1-mdoel

invoke列表:
active_record
-test_unit
影響文件夾:
db/migrate/20171018012356_create_xxx_xxx.rb
app/models/
test/models/
test/fixtures/

2-controller

invoke列表:
erb
test_unit
helper
-test_unit
assets
-coffee
-scss
影響文件夾:
app/controllers/
app/views/
test/controllers/
app/helpers/
app/assets/javascripts/
app/assets/stylesheets/

3-scaffold

invoke列表:
active_record
-test_unit
resource_route
scaffold_controller
-erb
-test_unit
-helper
--test_unit
-jbuilder
assets
-coffee
-scss
scss
影響文件夾:
db/migrate/20171018012356_create_xxx_xxx.rb
app/models/
test/models/
test/fixtures/
config/ #routes.rb resources :accounts
app/controllers/
app/views/accounts
test/controllers/
app/helpers/
app/views/accounts/ #.json.jbuilder
app/assets/javascripts/
app/assets/stylesheets/

4-migration

invoke列表:
active_record
影響文件夾:
db/migrate/20171018012356_xxx_xxx.rb

<<總结>>

scaffold生成文件最多,影響範圍大,invoke關键點包括5大部分:model\resource_routes\scaffold_controller\assets\scss. 文件類型包含6種:rb\yml\erb\jbuilder\coffee\scss.

scaffold可以看作是包含了MVC的集合體,框架的框架,最小可用的最多樣板代碼文件的集成.

migration是用來修改或修正model的,可以看做是一種特殊的model.

model\scaffold\migration這三個有共同點,都調用了active_record並且都生成了db/migrate文件夾下面數字開頭的的rb文件.

所以都要需要 [field[:type][:index] ...参數,其中的type包括以下12種.

field[:type]字段類型列表

integer
primary_key
decimal
float
boolean
binary
string
text
date
time
datetime
timestamp

← Rails 百寶箱第三集 19-1上傳單張圖片出現的 bug
 
comments powered by Disqus