Custom Scaffold for Rails
It 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 %>
<% 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]
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.
sweet…this looks cool. tables? omg. that’s soooo 1997. DIVs and CSS paaaaalease. heheh.
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.
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.
@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.
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.
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.
Tables are great when used for what they are intended, showing data in a tabular form.
Anything similar for editing scaffold forms.
Could you please show us how this could be used with ActiveScaffold?
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.
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
nice plugin,
i’d suggest wrapping table header row with rather than as per the standard.
keep the hard work!
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.
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.
Hi.
Good design, who make it?
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
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…
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
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?