Rails 2.0 Scaffolding

I’m learning Ruby on Rails starting with 2.0. This is occasionally problematic, as it was only released a few days ago, and the tutorials are still all for 1.2.

So, to help others, something not mentioned in the release notes, which causes errors if you’re following the official tutorial.

Scaffolding has changed.

The 1.2 way was to stick scaffold :modelname into a controller.

The 2.0 way is to run ./script/generate scaffold ModelName field1:type field2:type field3:type on the command line.

The new way is more useful, I think, as it reduces the initial hurdle of moving from a scaffolded controller to a slightly custom one. It gives you controllers filled with code ready to be tweaked, and sets up a migration to create your model. You’re left with a working site filled with examples of how to do things in rails, instead of a magical “scaffold :foo”.

It’s just that it’s a wee bit undocumented.

An aside: I felt that I needed to post this because googling for “rails 2.0 scaffolding” didn’t actually produce anything helpful on the first page or so. Lots of talk about whether it’s ideologically pure, but not so much on the “this is how to do it” front.

[Update: I get the impression, from seeing others talk about this, that it’s not so much that scaffolding has changed as that one scaffolding option has been removed. It looks like the scaffold generator was there pre-2.0, and the only change is that scaffold :Foo is no longer available. Still breaks every “getting started with Rails” article I’ve ever seen, though. 😛 ]

11 thoughts on “Rails 2.0 Scaffolding”

  1. Wow, great simple post. I’ve had the same situation n problem and have been looking all around for the correct version of scaffolding for 2.0. Tried script/generate scaffold name but failed. Now I got it.


  2. Hay David,

    Thanks for stopping by my site.

    Let me assure you, I’m in no way only into the philosophy of the design of the framework.

    Quite to the contrary, my complaint is rooted in a simple and pragmatic use case that you and your readers might recognize.

    The problem with the new scaffolding isn’t in the generation‘s command-line semantics. In fact I think that’s much improved and by convention-based scaffold-naming and attribute enumeration saves you a bunch of typing.

    The thing that’s hairy, and you might come across this later, it’s what happens when you decide you need a new column, or a new association?

    Under the old release you would describe the column in your migration, run a ‘rake db:migrate’ and violá you would have it automatically integrated into your scaffolded view.

    The reason I go on about this is not because I love scaffolds like so much, but because I like the ability to “sketch”: to try something out, to say, “Hm, no, maybe not”. You didn’t have to edit all of the scaffold files and there was no lock-in. This “scaffold :modelname” was a great way to sketch.

    Under Rails 1.x when you DID want that lock-in, all you had to do was issue a command to create the static files based on your “now-perfected migration” and ( script/generate scaffold model controller ) then you could modify the generated (new/edit/show/index) views (see: AWDWR p.77).

    I think you had more flexibility, and that’s what I was a bit upset about losing…or, as this is my first time back at Rails after a hiatus from Rails, I thought maybe that same flexibility had been preserved, but the way to code it had been changed.

    Evidently it’s the decision of the Rails senior management that that super-flexible first phase needed to go away and yield to that second mode being theonly mode.

    To me, you had the best of both worlds, and now there’s a feature loss, and that’s what drove the post you linked to.

    If you want to track the process from “here’s what I was used to” to “Oh so it works like this”, which is before the page you linked to, you can follow the learning process ( and not have to type as much as I did! ). I was in Theresia’s shoes but a week ago and after dumping tons of time into finding out the scaffold had been lost, those posts were written.

    Best wishes on your voyage on the HMS Rails,


  3. The example you give of the scaffold generation process works, but it appears to generate a controller named the same as the model. I’m trying to find some documentation on the generation command to see if it will take the controller name you’d like to use. For example the Depot application in AWDWR you put the product model in the admin controller. Still searching for a way to do this.

  4. Matt: You can get documentation by just running the script with no arguments — “./scripts/generate scaffold” (It gives the most thorough overview of it I managed to find, at any rate.)

    It looks like pre-2.0 you could tell it the controller name, but the syntax to the command got totally changed.

    I think that if you want to scaffold a controller whose model-name is different to the controller’s then you need to generate it and then just rename it. I think all you need to change is the filename and the map.resources :foo in routes.rb. (Or maybe not. I haven’t tried…)

  5. Thank you very much for this post! I was having similar problems, and the documentation on this new “feature” is still sparse at best. But now I know what’s up with the scaffold. Thanks!

  6. Hi David,

    thanks for your post. Saved me several hours++ of wondering, while even a German RoR book here that claims to be RoR2.0 ready, shows old syntax and never gets it straight.


  7. Hi David,
    nice tip, thanks.
    What about migrations on the database after you generate the scaffold?
    For example I want to add field4:type to ModelName. I should do:

    $ ./script/generate migration add_field4

    Then open the db/migration/file_that_refers_to_my_migration and write “add_column :model_name :field4, :type” in “self.up” and “remove_column :model_name :field4 in “self.down”.
    Now, do I have to edit ModelName’s view in order to be able to edit, show and create a new ModelName entry in my database including field4? Or there’s a way to re-scaffold the model migrated?

    Thanks a lot.

  8. The book I am learnign from has error with relation to this,, I am learning on 2.0 and the book refers to 1.2 this seems to clear up some of the issues I have! Thanks

  9. I understand the value of going to a command line only way of generating scaffolding, but to remove the optional controller name from the syntax is brain dead. Rename all the files? If we are supposed to use things like generators to save time/work, then we should be able to specify the appropriate parameters.

  10. Khello my american friends , my name be Borat Sagdiyev .. I adore yall , do y’all like me. I greatly admire your blog . I did seen it on astana’s one supercomputer . an IBM 386 laptops .. Would you take me to view Mrs Pamelas ?

Leave a Reply

Your email address will not be published. Required fields are marked *