Scaffolding in Ruby on Rails refers to theauto-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( C reate, Read, Update, Delete) web interface for the Story table using the following command:
$rails generate scaffold Story title:string content:text
It's way easier to do this, instead of coding everything yourself, it saves you a lot of time!
Scaffold or Models?
Compared to thescaffold that generates everything that you need (and do not need),modelscreate only some related components. The best, and my favorite, way to explain the difference between scaffold and models is by using the following example:
Once you enter the command $rails generate model Story title:string content:textyou will generate:
invoke active_record will tie your model to the database, while the next line createsa migration file. Migrations are used to alter your database schema. This migration file creates the database table called 'stories', and database columns for "title" and "content ".
The third line will create a model - a Ruby class that inherits the Active Record. With this, every method that can be called in Active Record can now be called in your model. The last three lines create relatedtest files for your model.
While scaffolding will get you up and running quickly, the code it generates is unlikely to be the 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. You can read more about this on RailsGuides .
As you can see, generate modelsdoes not create any kind of view to display information on a page. To have a complete, ready to use application, you would need to:generate controllers (which in turn generates views, as well) or
generate scaffold (which generates your model, views, controller and writes to your routes.rb file).
If you change your mind and decide to use scaffold after already generating the model, you can always run$rails generate scaffold
If you enter the command $rails generate scaffold Story title:string content:textyou will generate the following files:
Once model related tests are created, the next line will generateresource routesto your stories. After generating resource routes comes thecontrollerand its actions (index, show, new, edit, create, update and destroy), together withviewsfor each of this actions.
The Rails router recognizes URLs and connecting them to a controller's action. By default, a controller's action will render a view of the same name.
Every time you create a migrationusing scripts (generate model / generate scaffold) a new migration is added to the correct directory. Youuse$rake db:migrateto checks which migrations that have not been added to the database.
== 20180205103508 CreateStories: migrating ====================================
== 20180205103508 CreateStories: migrated (0.0008s) ===========================Destroy
Everyone is free to edit and do the necessary changes to their application to work as intended, even if it means completelyremoving scaffold. You can remove scaffold in the following way:Generate scaffold:$rails generate scaffold Story
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.
A scaffold is excellent to use when it comes tosimple examples, quick mockups or testing. However, be sure to generate your own models when you decide to develop a new application.
We hope you discovered something new today!
Originally posted on kolosek.com.