Rails 1.2 is out now of course. As most of you know the big feature in 1.2 is more support for “automatic CRUD style controllers”. The big feature here is the scaffold_resource generator. As documented elsewhere (but not that extensively) you invoke

ruby script/generate scaffold_resource <modelname> [attributename:attributetype]

where the optional attributename (or more than one of them) identifies an attribute of the model. The controller is generated for you with the same name.

The Good News

So, this is a great step forward. It promotes a REST style of design and the direction that Rails is moving. Its a very good thing that Rails is not agnostic about the right way to build apps. Just as Rails puts a stake in the ground and says that you essentially must build apps using MVC, using capabilities like this does the same to enforce and encourage REST.

More pragmatically, it saves you from writing those REST style controllers yourself, something I found myself doing in app after app. So I was very excited to see this feature and looking forward to saving some typing and being even DRYer.

Some Problems

But, alas, if you are starting from an existing table, you’ll immediately see the problem with this approach. If you don’t list your attributes explicitly (which those of you used to working with existing tables are not used to doing) you will get a surprise when you first invoke your controller actions(with URLs such as http://localhost:3000//view/1 or http://localhost:3000//new). When you view or add any particular record the forms will be empty. Of course, you can go look at your database table schema and just add several column name arguments to your call to the scaffold_resource generator. But that doesn’t seem very DRY, assuming that at some point you created that table (although its DRY enough if you don’t yet have the table).


The best fix is probably that the forms introspect the model to find out its attributes and then display those attributes. This code can be placed once in the form.rhtml partial. Note that 1.2 doesn’t actually have that partial. But it appears to be in a fix put into Edge Rails, so it will probably be available in nearterm update to 1.2. Hopefully the form partial can include the dynamic model attribute display, at least as an option.

In the meantime, I may write a little scaffold_resource script generator “invocation generation” script that reads attributes to generate from the underlying table.