How to customize the default scaffold?

I know the scaffold generator is really only useful for beginners and/or very the preliminary state of a project, but I find that I use it a lot (especially, since I am still a beginner). There are a couple of things I find myself tweaking (or wanting to tweak, but never getting around to tweaking) each time I generate a new scaffold:

1) Change the layout from blah.html.erb to application.html.erb, and delete blah.html.erb (errr, well, I copy the first one over, change the heading, and then delete it.) 2) Change the new and edit templates to render a common partial (a la Akita on Rails' excellent article Rolling with Rails 2.0 - The First Full Tutorial - Part 1 | AkitaOnRails.com)

I suppose I could write my own scaffold generator (copying most of it from the existing scaffold generator).

Or I could modify the existing scaffold generator to allow for user defined enhancements, and possibly even do so in such a manner as to be able to submit it upstream as a patch/enhancement.

Or I could ask if such a feature already exists.

I'm starting with the last option :slight_smile:

--wpd

You want to take a look at Ryan Bates's Railscasts #058 "How to make a generator"

I did exactly that back in June 2009 when I had a clean slate project that was going to have several vanilla models for an admin back-end. Scaffolding is great for that, but like you I always had a handful of tweaks. With my own "my_scaffold" generator, the layout has the changes I want, the controller actions do what I want (and redirect where I want), and I only had to make the changes once. (--to the generator templates of course)

-Rob

Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com

Hi Patrick,

I had the same idea a few months ago, and wrote a gem called “View Mapper” which does exactly this. See: View Mapper: Scaffolding for your models and plugins - Pat Shaughnessy or: http://github.com/patshaughnessy/view_mapper

The idea is that View Mapper will generate different variations of scaffolding code (called “views”) for different purposes: scaffolding + auto_complete, scaffolding + file attachments using Paperclip, scaffolding for two models in a has_many/belongs_to relationship, etc. Each of these variations is implemented by a “View Mapper Module” – the idea is that anyone could write such as module; I’m planning on writing a tutorial soon on how to do this.

Anyway, to make all of this possible I found a simple way to extend the standard Rails scaffolding without just copy/pasting the entire thing over and over again. Check out the code on github to see what I mean… and/or drop me a line if you’re interesting in learning more about the details.

- pat http://patshaughnessy.net