The Fall from Scaffolding
I learned about Ruby on Rails because I was into Basecamp. Basecamp I heard about on a designer’s forum called YayHooray!, a long time ago, back when Basecamp was new and beta. If I hadn’t read that Rails was what was behind Basecamp, I probably never would have bothered looking into it.
But what really sucked me in, once I gave Rails a few minutes’ chance to impress me, was the video. I’d never seen anything like it. The demo dude—who I later learned was David Hansson himself—created a very simple, but functional, web app in a mere 10 minutes or less.
Being as I was in the depths of PHP hell—working on custom components and hacks for Mambo—the Rails video felt like a divine vision. If a magical “ahhh!” and heavenly light had shone on my PowerBook screen, it wouldn’t have seemed out of place.
But no divine light appeared, and I had to go fetch some paper towels to wipe up the small puddle of drool that had accumulated on the floor. Even ten minutes of saliva production results in more volume than you might otherwise think.
So of course, once the floor was nice and clean—if a bit shinier than before—I did what any self-respecting geek would do: I went and scrounged up every installation tutorial I could, and pestered folks in the
#rubyonrails IRC channel when I got stuck. (I’m fairly handy with a command-line, but really, it was a much tougher thing back then!)
What impressed me so was the scaffolding, the automatic views and code generated by Rails taking a peek into your database models and making some simple assumptions. The forms weren’t pretty, but they did work, and you could list, view, edit, and delete items with just a flick of the wrist.
Back then you started off your scaffolded web app like thus:
PeanutsController < ActionController::Base scaffold :modelname end
In my first article on Rails, excited newbie I was, I mused that it would be fun to get ahold of the autogenerated code Rails uses to scaffold so I could get a better understanding of what was going on. It was cool, but very, very “magical” and not necessarily in a good way. At the time, I didn’t know that you could simply do this instead, and get editable code:
script/generate scaffold peanuts
You probably started out with this command, though, as the
scaffold method has been phased out over time. So you’re probably familiar with the results—a
peanuts_controller.rb file in the
controllers directory, a
peanut.rb file in the
models directory, and a bunch of view files in the
As soon as the initial delight wore off, though, I felt at odds. I realize that I’m not alone in this—it seems like most people have gone through Scaffold Withdrawal of a sort. It’s that cold, clammy realization that scaffolding can’t cut it forever and we don’t know what to do next.
We’d love everything to be just that easy, of course, but a nagging little voice somewhere says “Dude, you have to be able to actually create associations between your models. You can’t do this with scaffolding. You have to actually learn something.”
On the flip side, some people cope with their Scaffold Withdrawal by figuring that scaffolding must have magical properties they haven’t yet found. They ask people “How do I make scaffolding do x, y, and z?” They are in denial, but that is a normal part of the withdrawal/grieving process. Just don’t let yourself get stuck there forever.
You’ve gotta move on, man. Really. You’ll feel better.
The solution is a radical one, but bear with me:
You actually have to learn Rails.
The initial promise of no-work web apps is enticing, but unintentionally misleading. There’s a lot to learn about Rails. But it doesn’t have to be hard.
What you need to do, specifically, is learn how to write your own view templates—or expand/redesign the ones generated by scaffold—and add controller logic to take incoming data from view forms and mush ‘em together where there are associations.
Here are some links to get you going:
- A Little Form Help From Your Friends my article on form helpers
- Form Helper Cheatsheet mine, again, with concise form helper info
- Associations Rails Wiki page
- Typo Download the source, and check out the way it does stuff in views and controllers—specifically the admin stuff
And remember, the Rails community is a friendly one. There are lots of people out there happy to help you, including me.
But please leave behind bad habits developed in PHP-land, and don’t ask questions that have to do with specific tasks rather than outcomes. Don’t ask, “How do I write a forum?” because nobody wants to answer that question. Ask, “How do I link multiple models together in my controller code?” You’ll be much more likely to get assistance.