Ruby on Rails Custom ScaffoldingIt has been said so many times that scaffold generator is only useful for learning how Rails works and maybe for rapid prototyping and I certainly agree. But scaffold is one of the big things that really excited me when I first discovered Rails.

Why is Scaffold Generated Code Useless?

Actually I think the stubbed out functions in the controller as well as most of the views are already a great time saver but its really the generated list and show code that is useless:

 

Iterating over columns means that I can do nothing but rip this out and explicitly write out each column so I can then change the look and feel to be consistent with my app. Also, what if I want to use div’s instead of tables? Rip it apart.

Keeping a standard look and feel throughout your site is good UI design. However, the manual process of writing all your views and often using the same look and feel for list, form, show, new, and edit is very much against the DRY principle and there is no reason why this laborious task can’t be generated.

So we’ve written a simple custom_scaffold generator. To use this plugin just unzip it in vendor/plugins, and run:

./script/generate custom_scaffold [modelname]

It’ll generate scaffolding but instead of iterating over column names you’ll end up with something like this:

And that is something you can actually work with! I was going to build a templating system but I found that its quite easy to change the look and feel straight in the plugin templates files (vendor/plugins/custom_scaffold/generators/custom_scaffold/templates/)

I’ve also replaced the normal scaffold code for booleans which generates select lists (that don’t work by the way…. they forget their state during validation) with working checkboxes which make more sense for boolean to me.

Questions? Suggestions? Feedback?

Download custom_scaffold generator


Share it:
These icons link to social bookmarking sites where readers can share and discover new web pages.

  • Digg
  • del.icio.us
  • DZone
  • Ma.gnolia
  • Netscape
  • Reddit
  • StumbleUpon
  • YahooMyWeb