In my last two posts, I shared how I use Ryan Bates’ Nifty Generators gem to create starter layouts for my Rails applications and add a configuration file with variables for use throughout my app. While extremely useful,
nifty_config probably aren’t utilities you’ll make use of day in and day out as your application evolves. With each new model you add to your app, though,
nifty_scaffold will come in useful.
The default scaffolds in Rails are good to start, but they’ve got their shortcomings. It’s not hard to find criticisms elsewhere online, so I’ll just mention the two things that bugs me the most: First, they don’t generate un-DRY code, particularly for forms. Second, you’re stuck with Erb-formatted views and Test::Unit for a testing framework–if you want options, you’re kind of stuck.
So why is
nifty_scaffold better? Basically because it addresses those shortcomings. It’s much more flexible than what comes out of the box with Rails. Here’s a simple model to store information about restaurants–in this case, the name, location (city and state), and a description:
Reminder: I’m still using Rails 2.3; if you’re on Rails 3 use
rails generate nifty_scaffold instead.
So what does this generator give us? Let’s look at the list returned in the terminal. First, it’s created my model and the migration to go along with it:
It’s also created test files for Test::Unit (you can override this with RSpec or Shoulda if you prefer):
Next up is the controller, helper, and nice DRY views (notice the
_form.html.erb partial, which is referred to by both new and edit):
Finally, your RESTful routes:
Run your migrations, and you’re ready to go.
If you’re like me and prefer Haml to Erb, you can render your view templates to suit by including
--haml at the end of your command:
You can also tell the generator to create some default model and controller specs in RSpec (or Shoulda) instead of the default Test::Unit by passing along
Finally, you can automatically stage the new files for your next commit to a Git repository by passing
--git. I personally don’t do this because I usually tweak a few things prior to staging.
Of course, you can pass along any or all of these flags as needed–you don’t need to pick one.
That’s just a bit of what you can crank out with
nifty_scaffold, but there are several more flags it provides. I don’t use these very often myself, but it’s possible to do things like only create a model (or only create a controller and views), omit Rails’ default timestamps from your model (to be fair, regular scaffolds can do this as well), or indicate specific methods your controller needs (or does not need). The best way to see everything you can accomplish with
nifty_scaffold is to check out its help file:
Like with regular Rails scaffolds, if you’re not sure what a given call to
nifty_scaffold is going to do, pass
--pretend along with the rest to see a list of which files will be added or modified by the scaffold. For example,
That wraps up our tour of Nifty Generators. As I’ve been saying, the three components of this gem are extremely useful for Rails developers of all skill levels–they’re easy to follow for newcomers, and great timesavers for more advanced users who would prefer to get on to the more interesting aspects of their Rails applications.
I stand with the Black community against systemic racism, police violence and brutality, intolerance, and hate in the United States and worldwide. We must all demand better from our leaders, and ourselves. Stop tolerating intolerance.
While you're here, please consider making a donation to Black Girls CODE, who do great, important work to provide opportunity to underprivileged girls interested in tech, or any organization working toward equity and safety for all, not just the privileged. Thank you.
If you liked my series on practical advice for adding reliable tests to your Rails apps, check out the expanded ebook version. Lots of additional, exclusive content and a complete sample Rails application.
Ruby on Rails news and tips, and other ideas and surprises from Aaron at Everyday Rails. Delivered to your inbox on no particular set schedule.