SCAFFOLD: A QUICKEST WAY OF BUILDING A BLOG WITH POSTS AND COMMENTS

Scaffolding in Ruby on Rails refers to the auto-generation of a set of a model, views, and a controller usually used for a single database table. For example, you can auto-generate a ready to use controller, model, and views with a full CRUD ( Create, Read, Update, Delete) web interface for the Story table using the following command:

  1. Scaffolding was made popular by the Rails framework.
  2. When line scaffold :model_name is added to a controller, Rails will automatically generate all the appropriate data interfaces at run time.
  3. An external command can also be used to generate Ruby code for the scaffold in advance, which is rails generate scaffold model_name. The generated script will produce files of Ruby code that application can use to interact with database.

It’s way easier to do this, instead of coding everything yourself, it saves you a lot of time!

Lets Jump in to action

  1. The first thing we need to do is to create the Rails application:
    $ rails new blog
    

    2.In the blog app directory, use the scaffold generator to create the MVC components needed for posts and comments:

    $ cd blog
    $ rails generate scaffold post title:string body:text
    $ rails generate scaffold comment post_id:integer body:text
    

    it will generate the following action

     invoke  active_record
       create    db/migrate/20180522054949_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  test_unit
       create    test/system/posts_test.rb
       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
    

    3.Create the post and comment database tables:

    $ rails db:migrate
    

    it will generate

    = 20180522054949 CreatePosts: migrating ======================================
    -- create_table(:posts)
    -> 0.0009s
    == 20180522054949 CreatePosts: migrated (0.0009s) =============================
    
  2. In order to view the routes
    $ rake routes
    

    it will shows the routes for ur application

    Prefix Verb   URI Pattern                  Controller#Action
     comments GET    /comments(.:format)          comments#index
              POST   /comments(.:format)          comments#create
     new_comment GET    /comments/new(.: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/new(.: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
    
  3. In order to run the appication :
$ rails s

Warning

While scaffolding will get you up and running quickly, the code it generates is unlikely to be a perfect fit for your application. You’ll most probably want to customize the generated code. Many experienced Rails developers avoid scaffolding entirely, preferring to write all or most of their source code from scratch.

Destroy

Everyone is free to edit and make the necessary changes to their application so that it works as intended, even if it means completely removing scaffold. You can remove scaffold in the following way: If you migrated your files, perform a rollback:

$rake db:rollback

Destroy or undo scaffold:

$rails destroy scaffold Story

By doing this, you will delete all the files created by the scaffold but additional changes that you may have done manually will not be removed.