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:
$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!
Compared to the scaffold that generates everything that you need (and don't need), models create 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:text you will generate:
invoke active_record will tie your model to the database, while the next line creates a 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 related test files for your model.
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. You can read more about this on RailsGuides.
As you can see, generate models does 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 <name>. It will create all the missing files.
If you enter the command
$rails generate scaffold Story title:string content:text you will generate the following files:
route resources :stories
Once model related tests are created, the next line will generate resource routes to your stories. After generating resource routes comes the controller and its actions (index, show, new, edit, create, update and destroy), together with views for each of this actions.
The Rails router recognizes URLs and connects 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 migration using scripts (generate model/generate scaffold) a new migration is added to the correct directory. You use
$rake db:migrate to checks which migrations that have not been added to the database.
== 20180205103508 CreateStories: migrating ====================================
== 20180205103508 CreateStories: migrated (0.0008s) ===========================
Everyone is free to edit and do the necessary changes to their application to work as intended, even if it means completely removing scaffold. You can remove scaffold in the following way:
1. Generate scaffold:
$rails generate scaffold Story
1. If you migrated your files, perform a rollback:
2. 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 to simple 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.