As a beginner of Rails, I often get confused about what generator generates what and the syntax of these commands. Below is a cheat sheet on the four main generators I find most useful.
What are they?
They build out standard features of certain parts of your rails application. To start any generator type
rails g or
rails generator followed by your command.
To find a list of all generators you can also type
rails g. *make sure you are in your rails app*
You will see a terminal output that resembles below. This article will focus on the bolded generators: controller, migration, model, and resource.
-h, [--help] # Print generator's options and usage
-p, [--pretend] # Run but do not make any changes
-f, [--force] # Overwrite files that already exist
-s, [--skip] # Skip files that already exist
-q, [--quiet] # Suppress status outputPlease choose a generator below.Rails:
There are a lot more as you can see and you can easily learn about them by entering
rails g <name of generator> in the terminal. Your terminal will output a short description of that generator.
Things to note
A lifesaver for me is that you can easily erase all traces of the generator you created by simply replacing the g in
rails g <generator name> to a d like so,
rails d <name of generator>. So don’t be afraid!
Also, note I added
—-note-test-framework in the below examples, this does not create test files. If you need test files simply omit this command.
Lastly, since summer is almost over and who doesn’t like ice cream?! I chose ice cream as my generator example. Eat up!
The migration generator makes it easy to create and edit tables. To create a migration table follow below. You can also add columns to your command. String is the default datatype, so flavor in the example below is the same as size:string.
rails g migration create_ice_creams flavor size:string scoops:integer
You will see the below output telling you your table was created!
Running via Spring preloader in process 57527
If you go into your db/migrate folder you will see the table set up! Now all you need to do is
rails db:migrate. Sweet!
To create a model generator type the following in your terminal. Just like the migration generator, you can include attributes.
rails g model IceCream flavor size scoops:integer --no-test-framework
The below output tells us our migration table was created with the attributes along with our model file!
Running via Spring preloader in process 77288
For the controller generator instead of adding attributes like the model, you can add controller actions.
rails g controller ice_creams index show --no-test-framework
Your output will show that you just created quite a few files and folders!
Running via Spring preloader in process 58112
route get 'ice_creams/index'get 'ice_creams/show'
Let’s see what we created!
- Our ice_creams_controller.rb with our index and show actions defined
- Routes for index and show
- A folder in the views folder called ice_creams with index and show html.erb files
- helper files
- stylesheets for the controller
This is the most powerful out of the four! You can also include attributes for your model.
rails g resource IceCream flavor size scoops:integer --no-test-framework
Your terminal output will look something like below
Running via Spring preloader in process 61311
route resources :ice_creams
Here we created…
- Migration table along with columns that we put as our attributes
- The model
- Controller with no actions
- Ice_creams folder in views (note: no action html.erb files)
- Helper files
- Stylesheets for the controller
- Resource Route for all CRUD actions
As you can see rails generators are very powerful and can save a lot of time if you know how to utilize them. Hope this helped and happy coding!