Rails Scaffold
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!
Scaffold or Models?
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 scaffolds and models is by using the following example:
Generate models
Once you enter the command $rails generate model Story title:string content:text
you will generate:
invoke active_record
create db/migrate/20180205103025_create_stories.rb
create app/models/story.rb
invoke test_unit
create test/models/story_test.rb
create test/fixtures/stories.yml
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, generating 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.
Generate scaffold
If you enter the command $rails generate scaffold Story title:string content:text
you will generate the following files:
invoke active_record
create db/migrate/20180205103508_create_stories.rb
create app/models/story.rb
invoke test_unit
create test/models/story_test.rb
create test/fixtures/stories.yml
invoke resource_route
route resources :stories
invoke scaffold_controller
create app/controllers/stories_controller.rb
invoke erb
create app/views/stories
create app/views/stories/index.html.erb
create app/views/stories/edit.html.erb
create app/views/stories/show.html.erb
create app/views/stories/new.html.erb
create app/views/stories/_form.html.erb
invoke test_unit
create test/controllers/stories_controller_test.rb
invoke helper
create app/helpers/stories_helper.rb
invoke test_unit
invoke jbuilder
create app/views/stories/index.json.jbuilder
create app/views/stories/show.json.jbuilder
create app/views/stories/_story.json.jbuilder
invoke assets
invoke coffee
create app/assets/javascripts/stories.coffee
invoke scss
create app/assets/stylesheets/stories.scss
invoke scss
create app/assets/stylesheets/scaffolds.scss
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 and controller tests for each of these 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.
Migration
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 check which migrations that have not been added to the database.
== 20180205103508 CreateStories: migrating ====================================
-- create_table(:stories)
-> 0.0007s
== 20180205103508 CreateStories: migrated (0.0008s) ===========================
Destroy
Everyone is free to edit and make the necessary changes to their application to work as intended, even if it means completely removing the scaffold. You can remove the 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.
Conclusion
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. Don't forget to always test your application, find out how to do it by using Capybara!
We hope you discovered something new today!
If you want to dig deeper and search for more Ruby mysteries, be sure to subscribe to our newsletter!