Rails Generators: Resource vs. Scaffold

If you were to create a “to do” list of all of the tasks required to build out CRUD functionality in an application, you would quickly learn that the process is quite…lengthy. Luckily, the main objective of the Rails creation team was to create an efficient way to build core application functionality — which is what the rails generate command exemplifies!

WHY USE RAILS GENERATORS?

Generators helps us save time when building out our Rails application. They can also (1) help us set up some basic specs for an application’s test suite, (2) alleviate us from creating bugs caused by syntax or spelling errors, and (3) ensure us that we are using Rails best practice.

Rails:
assets
controller
generator
helper
inherited_resources_controller
integration_test
mailer
migration
model
observer
performance_test
resource
responders_controller
scaffold
scaffold_controller
session_migration
task

NOTE: rails generate can be shortened to rails g .

The Resource Generator

After running rails g resource Client name username email password_digest, you will see that this generator produces some of the main functionality necessary for a full resource without unnecessary or long lines of code. When using this command, there aren’t many (or any, rather!) files that you would need to remove from your architecture:

invoke  active_record
create db/migrate/20210219173139_create_clients.rb
create app/models/client.rb
invoke test_unit
create test/models/client_test.rb
create test/fixtures/clients.yml
invoke controller
create app/controllers/clients_controller.rb
invoke erb
create app/views/clients
invoke test_unit
create test/controllers/clients_controller_test.rb
invoke helper
create app/helpers/clients_helper.rb
invoke test_unit
invoke assets
invoke scss
create app/assets/stylesheets/clients.scss
invoke resource_route
route resources :clients

Take note of the last item listed — resources :clients. It automatically creates each of the RESTful routes essential to perform CRUD actions and making them available to the controller.

The Scaffold Generator

Running rails g scaffold Client name username email password_digest will introduce you to a whole new world of what “Rails magic” is really capable of:

invoke  active_record
create db/migrate/20210219175033_create_clients.rb
create app/models/client.rb
invoke test_unit
create test/models/client_test.rb
create test/fixtures/clients.yml
invoke resource_route
route resources :clients
invoke scaffold_controller
create app/controllers/clients_controller.rb
invoke erb
create app/views/clients
create app/views/clients/index.html.erb
create app/views/clients/edit.html.erb
create app/views/clients/show.html.erb
create app/views/clients/new.html.erb
create app/views/clients/_form.html.erb
invoke resource_route
invoke test_unit
create test/controllers/clients_controller_test.rb
create test/system/clients_test.rb
invoke helper
create app/helpers/clients_helper.rb
invoke test_unit
invoke jbuilder
create app/views/clients/index.json.jbuilder
create app/views/clients/show.json.jbuilder
create app/views/clients/_client.json.jbuilder
invoke assets
invoke scss
create app/assets/stylesheets/clients.scss
invoke scss
create app/assets/stylesheets/scaffolds.scss

Looking above, we see that scaffold creates a model, database migration for that model, controller to manipulate it, views, and even a test suite for each! Although scaffolding can be a good way to get your project up and running, it is pretty unlikely that it will be 100% ideal to what you you had in mind for your application. You will have a lot of code bloat and will be left will manually deleting files and code.

~

It gets pretty old creating projects and writing the same code over and over again and setting up very basic CRUD functionality. Generators can save you plenty of time — if you choose the correct one! To check out the complete list of rails generators, type in the rails generate in your terminal.

Student at The Flatiron School / Software Engineering Immersive / Washington, D.C.