Rails generators are used to create many files for models, controllers, views, unit tests, migrations and more. It will do some of the manual work for us. So it saves time. They can also be used to set up some basic specs for the application test suite.
Running rails generate(or rails g) by itself gives a list of available generators:
$ rails generate
Usage: rails generate GENERATOR [args] [options]
The main generators that rails offers are:
- controller: The Rails controller coordinates the interaction between the user, the views, and the model.
- helper: Code in helpers is included in the corresponding view. We can move big loops, method calls or other logic into a method in the helper.
- mailer: It allows for sending and receiving emails from and to your application.
- migration: Rails Migration allows you to use Ruby to define changes to your database schema.
- model: It is Ruby class that talk to the database, store and validate data, perform the business logic.
- scaffold: It refers to the auto-generation of a simple set of a model, views and controller usually for a single table.
Following is the list of options, which can be used along with generators:
-h, [–help] # Print generator’s options and usage
-p, [–pretend] # Run but do not make any changes
-f, [–force] …