A drop-in replacement for Ruby on Rails’s scaffold generator
Note: My reasons for starting this project are somewhat moot because of the REST support built into Ruby on Rails v1.2. (I can’t rule out the possibility that this project helped inspire the SimplyRestful plugin that later became part of Rails, though!)
I now recommend that you use the Rails scaffold_resource generator instead of my trestle generator. I think you’ll recognize in it the spirit and goals of the trestle generator, and it produces a standards-based API for your application, to boot.
-- /\/. _/.
Everybody loves scaffolding, and with good reason. It’s the feature of the Ruby on Rails framework that makes a database table editable on the web in mere seconds, and with just a few lines of generated Ruby code.
The trestle generator is an adaptation of the Rails scaffold generator. It produces scaffolding that’s more like production-quality code while maintaining all the rapid goodness you know and love about Rails.
Don’t drive your train across a gorge with nothing but a scaffold underneath it. Use a trestle instead!
Trestles have two advantages over scaffolds:
- More elegant handling of HTTP POST and GET requests
- A streamlined URL structure that makes your application easier to use
Downloading and installation is a breeze with RubyGems, the package manager that’s included in most Ruby distributions. Just type the following at a command line:
gem install trestle_generator
Generating trestles for your database tables is also a one-step process:
script/generate trestle YourModelNameHere
Why switch from scaffolds to trestles? Safety
The trestle generator produces Rails views that use HTTP POST to request changes (instead of GET). It also generates Rails controllers that execute such changes only for HTTP POST requests. Well-behaved web applications must protect user data against unwanted changes, including those arising from errant HTTP GET requests such as Google Web Accelerator is known to make. The scaffolding provided by Rails v1.1 is better about this than Rails v1.0 was, but trestles solve the problem of GET vs POST more cleanly and comprehensively.
Why switch from scaffolds to trestles? Usability
Trestle controllers have just four actions (index, new, edit, and destroy). Contrast this with scaffold controllers which have eight actions to implement the same functionality. This is mostly a matter of taste, but Rails developers appreciate more than most that “less is more.”
Just as important as aesthetics is the fact that trestle controllers better reflect GET and POST semantics, making for more intelligible HTTP conversations in your applications. (Apparently, Tobias Lütke wrote a postback_generator RubyGem with a similar philosophy, but trestles take the concept further.)
Use trestles instead of scaffolding and you will find yourself throwing away and tweaking less code as you build out applications. As joyous as Ruby programming is, less code is still a good thing.
Let’s look at some of the differences between trestles and scaffolds. A scaffold for a database table named ‘people’ has the following HTTP interface:
|/people||Lists existing person records|
|/people/new||Shows an empty person form|
|/people/create||Creates a new person record from request parameters|
|/people/show/99||Shows the person record having ID 99|
|/people/edit/99||Shows a person form for the person record having ID 99|
|/people/update/99||Updates the person record having ID 99 using request parameters|
|/people/destroy/99||Deletes the person record having ID 99|
As of Rails v1.1, scaffold controllers reject requests to insert, update and delete records if the request is HTTP GET instead of POST. This is a safety improvement, but the design still leaves something to be desired.
Contrast this with the HTTP interface of the equivalent trestle controller:
|/people||GET or POST||Lists existing person records|
|/people/new||GET||Shows an empty person form|
|POST||Creates a new person record from request parameters|
|/people/99||GET or POST||Shows the person record having ID 99|
|/people/99/edit||GET||Shows a person form for the person record having ID 99|
|POST||Updates the person record having ID 99 using request parameters|
|/people/99/destroy||GET||Redirects to /people/99/edit with a notice that the user must click the form’s Destroy button in order to destroy a record|
|POST||Deletes the person record having ID 99 after prompting the user for confirmation|
Do yourself and your users a favor by switching from scaffolds to trestles.
Visit the RubyForge page for this open-source project. Contact me at nils at alumni dot rice dot edu.
-- /\/. _/.