Rails Generators Make Coding Easier
To create a working application, you would need to create migration tables, create models, create routes, and configure your views. That is so much stuff, also any other features on top of that too! It is time consuming and a high chance of running into bugs. Luckily, rails generators are a fast and easy way to create all the features in an efficient way instead of manually write out each one. Rails generators are able to create many files or a feature just based on one line of code!
Once in a directory, you can get a list of all available generators by using rails generate
. This will give a list of all generators that comes with Rails. You can use rails generate helper
to give a detailed description of the helper generator. You will also be able to see any tags that can be added to generator, such as --no-test-framework
. This will create all the files the generator normally makes besides the test files.
The basic syntax for using a rails generator is rails generate <name of generator> <options>
. But, we are programmers, why would we write more when we could write less. The shortened way is rails g <name of generator> <options>
.
The four main generators used in my Flatiron school program are:
· Migration
· Model
· Controller
· Resource
Let’s go into detail about each one.
rails g migration
For a migration, we would have to make a ruby file, make the table, and write the attributes for the columns. That sounds exhausting. What if we forget an end, or misspell change. Luckily, we can just use rails g migration
with the attributes wanted to fill the migration.

The command above generated a whole migration with the three attributes listed :

No grammatical errors and no missing ends.
rails g model
The model generator not only creates the model, but the database associated with the model as well. I see it as the next step above rails g migration
. It does what the migration generator does and more. This generator also creates an Application Record model. Application Record model inherits from ActiveRecord:Base
. This gives us all the functionality of Active Record with added flexibility. This generator also pluralizes where it is needed to keep with the conventions used. So I do not have to worry about writing candies or candys (the answer is candies).

A candy spec file was also created. This is a test file to see if the model is working properly. The migration table is made with all the attributes listed on the line of code and an empty “Candy” model is created that inherits Application Record.
rails g controller
The controller generator creates the view files, test files, style files, and the controller files needed for the controller to properly function. This generator can make unnecessary files that are not used, such as the coffee file.

It is best practice to use the controller generator for static views or applications that do not need CRUD functionality. We can see that this command made a ton of files but some key files for CRUD functionality are missing. The model files any migration files were not created. Luckily, Rails has another generator that has those capabilities!
rails g resource
This controller generates everything you can ask for. The resource controller is a combination of the controller generator and the model controller. It creates the view files, test files, style files, and the controller files like the controller generator does as well as the model files and the migration files like the model controller does.

The resource generator also generates the all the routes for the candy controller to have full CRUD functionality. The resource generator is great for building applications with full CRUD functionality. It would have taken so long to create all these files, instead, I was able to create all of them with one line of code! I did not have to worry about any misspells or any missing ends. Overall, generators are great to use to save time on creating files that are a part of the core functionality of a web application and to reduce the amount of bugs that can occur.