In last few hours, I was creating some application that could work in near future as a blog. But before that good to make some nice structure to have a base of application. So this post is about: how to create a blog using scaffolding. Scaffolding is a quick way to generate some of the most important pieces of an application. If I want to create models, view and also controllers for a new resource in a single operation scaffolding is a great way to make the job quick and easier.
Creating a new rails app is very easy. To do that, I can type only:
rails new scaffold_app
The whole structure of app is as:
drwxr - xr - x 18 andrzejdubaj staff 612 20 lis 20 : 40 .
drwxr - xr - x 18 andrzejdubaj staff 612 20 lis 20 : 40 ..
- rw - r -- r -- 1 andrzejdubaj staff 543 20 lis 20 : 40 . gitignore
- rw - r -- r -- 1 andrzejdubaj staff 1729 20 lis 20 : 40 Gemfile
- rw - r -- r -- 1 andrzejdubaj staff 4330 20 lis 20 : 40 Gemfile . lock
- rw - r -- r -- 1 andrzejdubaj staff 374 20 lis 20 : 40 README . md
- rw - r -- r -- 1 andrzejdubaj staff 227 20 lis 20 : 40 Rakefile
drwxr - xr - x 10 andrzejdubaj staff 340 20 lis 20 : 40 app
drwxr - xr - x 8 andrzejdubaj staff 272 20 lis 20 : 40 bin
drwxr - xr - x 14 andrzejdubaj staff 476 20 lis 20 : 40 config
- rw - r -- r -- 1 andrzejdubaj staff 130 20 lis 20 : 40 config . ru
drwxr - xr - x 3 andrzejdubaj staff 102 20 lis 20 : 40 db
drwxr - xr - x 4 andrzejdubaj staff 136 20 lis 20 : 40 lib
drwxr - xr - x 3 andrzejdubaj staff 102 20 lis 20 : 40 log
drwxr - xr - x 9 andrzejdubaj staff 306 20 lis 20 : 40 public
drwxr - xr - x 9 andrzejdubaj staff 306 20 lis 20 : 40 test
drwxr - xr - x 4 andrzejdubaj staff 136 20 lis 20 : 40 tmp
drwxr - xr - x 3 andrzejdubaj staff 102 20 lis 20 : 40 vendor
To create an MVC components needed to post comments in the scaffold app directory we need to use scaffold generator:
rails generate scaffold post title :string body :text
rails generate scaffold post title :string body :txt
Running via Spring preloader in process 5067
invoke active_record
create db / migrate / 20161120194146 _create_posts . rb
create app / models / post . rb
invoke test_unit
create test / models / post_test . rb
create test / fixtures / posts . yml
invoke resource_route
route resources :posts
invoke scaffold_controller
create app / controllers / posts_controller . rb
invoke erb
create app / views / posts
create app / views / posts / index . html . erb
create app / views / posts / edit . html . erb
create app / views / posts / show . html . erb
create app / views / posts / new . html . erb
create app / views / posts / _form . html . erb
invoke test_unit
create test / controllers / posts_controller_test . rb
invoke helper
create app / helpers / posts_helper . rb
invoke test_unit
invoke jbuilder
create app / views / posts / index . json . jbuilder
create app / views / posts / show . json . jbuilder
create app / views / posts / _post . json . jbuilder
invoke assets
invoke coffee
create app / assets / javascripts / posts . coffee
invoke scss
create app / assets / stylesheets / posts . scss
invoke scss
create app / assets / stylesheets / scaffolds . scss
And this generator creates post:
rails generate scaffold comment post_id :integer body :text
rails generate scaffold comment post_id :integer body :text
Running via Spring preloader in process 7851
invoke active_record
create db / migrate / 20161120232019 _create_comments . rb
create app / models / comment . rb
invoke test_unit
create test / models / comment_test . rb
create test / fixtures / comments . yml
invoke resource_route
route resources :comments
invoke scaffold_controller
create app / controllers / comments_controller . rb
invoke erb
create app / views / comments
create app / views / comments / index . html . erb
create app / views / comments / edit . html . erb
create app / views / comments / show . html . erb
create app / views / comments / new . html . erb
create app / views / comments / _form . html . erb
invoke test_unit
create test / controllers / comments_controller_test . rb
invoke helper
create app / helpers / comments_helper . rb
invoke test_unit
invoke jbuilder
create app / views / comments / index . json . jbuilder
create app / views / comments / show . json . jbuilder
create app / views / comments / _comment . json . jbuilder
invoke assets
invoke coffee
create app / assets / javascripts / comments . coffee
invoke scss
create app / assets / stylesheets / comments . scss
invoke scss
identical app / assets / stylesheets / scaffolds . scss
Creates post and comments tables in database:
rake db :migrate
Rails uses rake commands to run migrations:
Prefix Verb URI Pattern Controller #Action
comments GET /comments(.:format) comments#index
POST / comments ( . :format ) comments #create
new_comment GET /comments/ne w ( . :format ) comments #new
edit_comment GET /comments/ :id / edit ( . :format ) comments #edit
comment GET /comments/ :id ( . :format ) comments #show
PATCH /comments/ :id ( . :format ) comments #update
PUT /comments/ :id ( . :format ) comments #update
DELETE /comments/ :id ( . :format ) comments #destroy
posts GET /posts(.:format) posts#index
POST / posts ( . :format ) posts #create
new_post GET /posts/ne w ( . :format ) posts #new
edit_post GET /posts/ :id / edit ( . :format ) posts #edit
post GET /posts/ :id ( . :format ) posts #show
PATCH /posts/ :id ( . :format ) posts #update
PUT /posts/ :id ( . :format ) posts #update
DELETE /posts/ :id ( . :format ) posts #destroy
Rake routes are the lists of all the URLs currently uses in the application.
== 20161120235256 CreatePosts : migrating ======================================
-- create_table ( :posts )
-> 0.0335 s
== 20161120235256 CreatePosts : migrated ( 0.0336 s ) =============================
== 20161120235304 CreateComments : migrating ===================================
-- create_table ( :comments )
-> 0.0162 s
== 20161120235304 CreateComments : migrated ( 0.0163 s ) ==========================
Rails scaffold simple app
After running rails server we can see that some very simple post and comments formulas were created. Good idea is to add some user with some unique id – integer type, public name – string type, email address – string type that will be used as a username. id, name, email attributes are columns in users table in a database.
Running via Spring preloader in process 9956
invoke active_record
create db / migrate / 20161121001633 _create_users . rb
create app / models / user . rb
invoke test_unit
create test / models / user_test . rb
create test / fixtures / users . yml
invoke resource_route
route resources :users
invoke scaffold_controller
create app / controllers / users_controller . rb
invoke erb
create app / views / users
create app / views / users / index . html . erb
create app / views / users / edit . html . erb
create app / views / users / show . html . erb
create app / views / users / new . html . erb
create app / views / users / _form . html . erb
invoke test_unit
create test / controllers / users_controller_test . rb
invoke helper
create app / helpers / users_helper . rb
invoke test_unit
invoke jbuilder
create app / views / users / index . json . jbuilder
create app / views / users / show . json . jbuilder
create app / views / users / _user . json . jbuilder
invoke assets
invoke coffee
create app / assets / javascripts / users . coffee
invoke scss
create app / assets / stylesheets / users . scss
invoke scss
identical app / assets / stylesheets / scaffolds . scss
To user should be associated each post that user will create:
rails generate scaffold Micropost content :string user_id :integer
Running via Spring preloader in process 10518
invoke active_record
create db / migrate / 20161121004208 _create_microposts . rb
create app / models / micropost . rb
invoke test_unit
create test / models / micropost_test . rb
create test / fixtures / microposts . yml
invoke resource_route
route resources :microposts
invoke scaffold_controller
create app / controllers / microposts_controller . rb
invoke erb
create app / views / microposts
create app / views / microposts / index . html . erb
create app / views / microposts / edit . html . erb
create app / views / microposts / show . html . erb
create app / views / microposts / new . html . erb
create app / views / microposts / _form . html . erb
invoke test_unit
create test / controllers / microposts_controller_test . rb
invoke helper
create app / helpers / microposts_helper . rb
invoke test_unit
invoke jbuilder
create app / views / microposts / index . json . jbuilder
create app / views / microposts / show . json . jbuilder
create app / views / microposts / _micropost . json . jbuilder
invoke assets
invoke coffee
create app / assets / javascripts / microposts . coffee
invoke scss
create app / assets / stylesheets / microposts . scss
invoke scss
identical app / assets / stylesheets / scaffolds . scss
So, for now, we should have scaffold application with:
comments
post
users
and adding each of them to database.
Reference:
Scaffold (programming) - Wikipedia
MVC - Wikipedia
My site is free of ads and trackers. Was this post helpful to you? Why not