Introduction to Rails Generators

Petersampson
Nov 5 · 3 min read

Bored of re-writing the same code over and over? You’re a programmer, of course you are. This introduction to generators in rails gives you a brief overview of their powers and what to watch out for!

Rails generators are a shortcut to building your MVC framework and database, which can become extremely repetitive. Below is a list of the generators I have found to be very useful, how to call them and what tangible files they provide you with. In the images below g stands for generate and d stands for destroy (both can be used, ruby doesn't care)

MIGRATION

1.1 Terminal call for migration generator

What you get…

1.2 Migration file created

As you can see the table columns are not provided so these must be remembered and manually filled in. They can be called in the terminal as shown in the other example…

1.3 Terminal call for migration generator

The key part of this call is the ‘Add’ ‘To’ and ‘Ride’. Rails needs these que’s to understand what you are trying to achieve.

1.4 Migration file created

MODEL

2.1 Terminal call for model generator

This generator will get you a migration just like above, however it will have the table columns added in for you! It also creates a model file as seen below. Model relationships must be added.

2.2 Model file created

CONTROLLER

3.1 Terminal call for controller generator

Everything after ‘Artists’ is optional and dependant on your apps needs. A controller generator will create the ArtistController, all views specified and routes. One advantage this generator has over the resources generator is that our views files are created for us.

3.2 Routes file created from controller generator

The main problem with the Controller generator is that all our routes are created as GET’s. These can be manually changed and is still quicker than hard-coding everything it has created. This leads us to our last generator which handles this problem.

RESOURCE

4.1 Terminal call for resource generator

Finally the resource generator, this generates the most code. It provides the migration, model, controller, views (folder) and routes files. Specifically generating all routes GET, POST, PATCH and DELETE. It also does it in a much more DRY and cleaner way as shown in the one line below.

4.2 Routes file created from resource generator

One downfall is that our views files are not created but this is a fairly quick task in comparison.

Generators are very helpful tools and boost work efficiency a lot. However, using them carelessly can end in a mess with multiple overlapping files, migrations out of sync and our RESTful routes simplified to only GET’s. Here’s some things to remember when working with them:

⏦ Controller methods must still be edited!

⏦ db:migrate and db:seed!

⏦ How to undo your changes… ‘rails destroy thing_you_just_created’

Finally, here is a list of all other generators that rails offers, if you found this post interesting or helpful, please go and look at these further:

assets

generators

helper

inherited_resources_controller

integration_test

mailer

observer

performance_test

responders_controller

scaffold

scaffold_controller

session_migration

task

Petersampson

Written by

Software Engineer based in London.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade