Edited at

Ruby On Rails - ②Scaffold


Scaffold使ってみる


Blogプロジェクト生成


Scaffoldコマンド

$ rails generate scaffold モデル名 カラム名1:データ型1 カラム名2:データ型 2 …

簡単なブログを作って見ます。Terminalから以下のコマンドを実行

$ rails generate scaffold blog title:string blog:text write_date: datetime

データ型

mysql
sqlite
postgresql

:binary
bolb
bolb
bytea

:boolean
tinyint(1)
boolean
boolean

:date
date
date
date

:datetime
datetime
datetime
timestamp

:decimal
decimal
decimal
decimal

:float
float
float
float

:integer
int(11)
integer
integer

:string
varchar(255)
varchar(255)
character varying(256)

:text
text
text
text

:time
time
datetime
time

:timestamp
datetime
datetime
timestamp

ソースが自動的に生成される

Running via Spring preloader in process 17329

invoke active_record
create db/migrate/20190324031749_create_blogs.rb
create app/models/blog.rb
invoke test_unit
create test/models/blog_test.rb
create test/fixtures/blogs.yml
invoke resource_route
route resources :blogs
invoke scaffold_controller
create app/controllers/blogs_controller.rb
invoke erb
create app/views/blogs
create app/views/blogs/index.html.erb
create app/views/blogs/edit.html.erb
create app/views/blogs/show.html.erb
create app/views/blogs/new.html.erb
create app/views/blogs/_form.html.erb
invoke test_unit
create test/controllers/blogs_controller_test.rb
create test/system/blogs_test.rb
invoke helper
create app/helpers/blogs_helper.rb
invoke test_unit
invoke jbuilder
create app/views/blogs/index.json.jbuilder
create app/views/blogs/show.json.jbuilder
create app/views/blogs/_blog.json.jbuilder
invoke assets
invoke coffee
create app/assets/javascripts/blogs.coffee
invoke scss
create app/assets/stylesheets/blogs.scss
invoke scss
create app/assets/stylesheets/scaffolds.scss

migrate実行

rails db:migrate

== 20190324031749 CreateBlogs: migrating ======================================
-- create_table(:blogs)
-> 0.0013s
== 20190324031749 CreateBlogs: migrated (0.0014s) =============================