Rails Scaffold Generator
Utilizing Rails Scaffold Generator
Rails Scaffold is (Kinda) Scary
The first thing you should know about me is that when it comes to computing, I looooove shortcuts. I’ve had all the common Windows keyboard shortcuts memorized for as long as I can remember and once I got my Mac, it was one of the first things I brushed up on.
So it should come as no surprise that after learning Ruby, SQL, and Active Record the ‘long way’, rails generators felt like a godsend. Unfortunately, as the proverb popularized by the Spider-Man comics reminds us, “With great power comes great responsibility”. Making mistakes when using rails generators can be something like a rabbit hole and thus they should be used carefully.
What are Rails Generators?
Rails favors ‘convention over configuration’, meaning they have a certain way of doing things. Rails generators are a way to efficiently build core functionality and it sets up the same way every time (following convention). There are five main generators offered by Rails:
- Migrations
- Models
- Controllers
- Resources
- Scaffold
But we’ll be focusing on the scaffold generator.
How to Use Rails Generators Properly
Being a self proclaimed shortcut aficionado, I dived right in…but things did not go as planned, it looked a little something like this:
Scaffold Generator
My thoughts on the scaffold generator are essentially this:
The scaffold generator will essentially build out the entire app for you; it creates a “full set of model, database migration for that model, controller to manipulate it, views to view and manipulate the data, and a test suite for each of the above” (The Rails Command Line).
Sounds great right? Not always, especially when you may not need all the functionality that scaffold provides. You could spend more time than it would have taken to hardcode, deleting what you don’t need.
As a someone newer to Rails, the scaffold generator creates a lot of things I not only don’t need, but also some I don’t even understand. Quite frankly, it can be fairly daunting to look at.
One of the most important things I’ve learned while coding is to work with either what you already know or make sure there are good resources for whatever you want to use. Thankfully, Rails has many resources spread far and wide over the internet so you would never hurt for help.
Takeaway
Learn from my mistakes, take it slow. Don’t abuse Rails generators (especially scaffold), find the one most appropriate for what you need to accomplish and if it’s not what you need don’t be afraid to:
$ rails destroy GENERATOR [arg]