Home > Code, Ruby on Rails > Custom Scaffold for Rails

Custom Scaffold for Rails

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:

 

[source:ruby]
<% for cat in @cats %>

<% for column in Cat.content_columns %>

<%=h cat.send(column.name) %>

<% end %>

<% end %>
[/source]

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:
[source:ruby]

<% for cat in @cats %>

<% end %>

Name Color Weight Is fat    
<%= cat.name %> <%= cat.color %> <%= cat.weight %> <%= cat.is_fat %>

[/source]

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

Categories: Code, Ruby on Rails Tags:
  1. Brent
    March 1st, 2007 at 15:57 | #1

    sweet…this looks cool. tables? omg. that’s soooo 1997. DIVs and CSS paaaaalease. :-) heheh.

  2. tony
    March 1st, 2007 at 16:17 | #2

    Pure CSS Rulez! You’ll see we put divs in for the show view.

    Actually tables do still have their place. After a lot of hacking we finally threw in the towel on a good div based form and went back to tables. It was just impossible to get the validation to look like I wanted it.

    Unfortunately my designer says there is no vertical align in CSS for td’s and valign is deprecated. Ugg. Tables still suck.

  3. March 2nd, 2007 at 10:06 | #3

    I guess I don’t really see a tremendous advantage to this approach as it still relies on hacking by the developer. IMO, scaffolding will always be just that — something that you put up to hold things up while you build the real thing. Personally, I get more mileage just by copying over an existing view template that implements my desired view already and then modifying as needed.

  4. tony
    March 2nd, 2007 at 10:20 | #4

    @Bill said:
    “I get more mileage just by copying over an existing view template that implements my desired view already and then modifying as needed.”

    This generator does exactly the same thing you are doing by hand. The difference between your method and my method is no typing. DRY.

  5. March 2nd, 2007 at 15:37 | #5

    I think this is better starting point for newbies, and also for not so newbies. Brent: Tables are not deprecated by CSS, it is that the should not be used to design a site but to show tabular data.

  6. March 3rd, 2007 at 08:55 | #6

    I see what you are saying; particularly given the line:
    “I found that its quite easy to change the look and feel straight in the plugin templates files”

    My comment really stemmed from my initial reaction; that is, in many cases, my more complex then a simple table. On hindsight, I do see where this could save some time and give the developer an additional leg up (over conventional scaffolding) when creating their views.

  7. March 5th, 2007 at 15:07 | #7

    Tables are great when used for what they are intended, showing data in a tabular form.

  8. Darrow
    March 10th, 2007 at 06:07 | #8

    Anything similar for editing scaffold forms.

  9. yitzhakbg
    March 25th, 2007 at 10:47 | #9

    Could you please show us how this could be used with ActiveScaffold?

  10. omnix
    April 7th, 2007 at 00:26 | #10

    Could you update this by replacing start_form_tag with the newer form_tag do block? start_form_tag / end_form_tag are deprecated in Rails 2.0. Thanks. Love the custom scaffold generator.

  11. eradani
    July 14th, 2007 at 20:50 | #11

    Uhhh… vertical align in TDs does very much exist in CSS. It’s vertical-align. For reference…

    http://www.w3schools.com/htmldom/prop_style_verticalalign.asp

    I personally prefer tables as I feel there’s a lot more control and much less jimmy-rigging between browsers that can get to be a real headache. But that’s me. For data display, they can’t be beat

  12. cmo-0
    July 26th, 2007 at 01:33 | #12

    nice plugin,

    i’d suggest wrapping table header row with rather than as per the standard.
    keep the hard work!

  13. cmo-0
    July 26th, 2007 at 01:36 | #13

    it seems the tags had been skipped. i meant in the previous post
    >>
    i’d suggest wrapping table header row with ‘th’ rather than ‘td’ as per the standard.

  14. August 8th, 2007 at 19:50 | #14

    Nice work. You might also be interested in http://www.streamlinedframework.org/ and http://activescaffold.com/ for other scaffolding type tweaks. I haven’t actually tried either yet but its on my todo list.

  15. naisioxerloro
    November 28th, 2007 at 21:26 | #15

    Hi.
    Good design, who make it?

  16. John
    December 10th, 2007 at 03:41 | #16

    I get this message:

    undefined local variable or method `unscaffolded_actions’ for #

    when I run custom_scaffold.

    Do I need to install anything else or is it not compatible with rails 1.99.1

  17. OsamaBinLogin
    December 17th, 2007 at 00:24 | #17

    yeah, I’m getting a lot of stuff not working as per docs. I’m using Rails 2.01 and staggering through oreilly’s Ruby on Rails Up and Running.

    rake migrate ==> rake db:migrate
    .rhtml ==> .html.erb
    scaffold :modname doesn’t work…

  18. December 23rd, 2007 at 14:39 | #18

    Don’t really have a comment about the plugin. Maybe it will give me a means to accomplsh my goal. I’m a noob to Rails and am soaking up as much as I can about all that it and it’s third party gems & plugins can do for me. Still learning the the whole views, partials, etc thing. But still don’t have a clue of what I am doing. Not even sure how to state my question. I’m working with active_scaffold. Perhaps a crutch. I love the fact that it gives me a fairly customizable view. I love being able to click on a column head for sort. And I link the spinner. But I want to control what it displays (the model i’ve scaffolded). Can’t figure it out. I want to simply filter out records that don’t meet my criteria with a button click and re-include them with a click of the same button. Right now I would be happy with a forced filter. Can’t get any responses from railsform or groups.google.active_scaffold forums. Again, as I said i’m a noob and perhaps I’m looking at this all wrong. I ran across this blog and found it interesting. I’m a Delphi/VB programmer and know how I would handle displaying a table and how to control it. Any feedback for this “deer in the headlights” would be welcome. Thanks again!

    TonyD

  19. Anthony Ettinger
    July 9th, 2008 at 16:07 | #19

    how did you go about creating the custom generator?

    Can we just create a copy of the ./generators directory and put it somewhere else to modify at will?

  1. No trackbacks yet.