Rails 2.0: Scaffold

Posted on December 21, 2007
Filed Under Rails, Ruby |

Following the Rails 2.0 hype, I’ve been playing around with it and decided to share a first impression: Scaffolding is gone!

But wait, before you knock your head against the wall, let me tell you something: I lied :)

Scaffolding is not really gone. It’s just changed a bit.

How can I tell? Well, as a good developer I thought: “I’ll just create a scaffold with the new version and see what’s different”

I fired up a terminal, created a news rails application and generated a new model:

$ script/generate model Contact name:string email:string - yes you can do this in rails 2.0, and these fields get into your model’s migration!

Now the I have a new model, it’s time for a controller to manage it:

$ script/generate controller Contacts

So your controller would look something like this huh?

class ContactsController < ApplicationController
scaffold :contact
end

Well, too bad! The method scaffold is gone from ActionController::Base! And I’m not lying this time!

Now that the dynamic scaffold is gone, we’re left with the static one.
Ok, let’s try it then:

$ script/generate scaffold contact

And it won’t work again! ;) At the end of the output, you will get something like this:

Another migration is already named create_contacts: db/migrate/001_create_contacts.rb

It really means that if your model is meant to be used by a scaffold, you better generate it in the same line. It will fail, afaik, if the model previously existed. Destroy yout model and controller, and execute the following:

$ script/generate scaffold Contact name:string email:string

Done! Just run your migrations, startup your server and your new scaffold in rails 2.0 will be working gracefully!

It took me a while to discover this changes because I didn’t find it well documented. But maybe I was not looking in the right places. :)

Comments

40 Responses to “Rails 2.0: Scaffold”

  1. lionslinger on December 22nd, 2007 6:12 am

    “Destroy yout model and controller”

    Do you just delete the files? i’m thinking of starting over from scratch…

  2. Leonardo Borges on December 22nd, 2007 3:20 pm

    No, you can destroy them just the way you do to create, like this:

    $ script/destroy model Task

    and

    $ script/destroy controller Tasks

    And you’re done, the right files get deleted.

    Hope this helps!

  3. Pete on January 10th, 2008 9:27 am

    This was a great little post… you saved me a lot of frustration and starting from scratch as well. I’ll be sure to remember the script/destroy bit (yah, I’m just starting my Ruby adventures). :)

    Cheers.

  4. Leonardo Borges on January 10th, 2008 9:52 am

    Hey Pete, thanks for the compliments. I’m happy this article helped you.

    Regards

  5. Pete on January 11th, 2008 11:24 am

    It sure did, I followed a link from another one of your posts at the ruby forum:

    http://www.ruby-forum.com/topic/136192#605871

    very useful post.

    Cheers.

  6. David Mantilla » Blog Archive » Learning Ruby on Rails on January 13th, 2008 5:42 pm

    […] Rails 2.0 blog post […]

  7. Mirjam on January 24th, 2008 2:54 pm

    It works ;o)), and I’m so happy that I finally figure it out, because I’m total newbie in ruby and rails. I don’t have any experience in scripting languages

  8. Rhonabwy » Ah! so that’s what happened… on January 27th, 2008 12:10 am

    […] on the ruby forums. In a nutshell, dynamic scaffolding doesn’t exist in Rails 2.x - but you can generate a static scaffolding with rails generator code.I’m certainly not quite used to “script/generate […]

  9. lasnite on February 1st, 2008 4:28 am

    Dude:
    I love you!!! I’m ready to give u first my born. Seriously, thanks for the help. It really lead me in the right direction.

  10. Wiley on February 2nd, 2008 6:28 pm

    Thanks for posting this. I’ve been trying to follow the Agile Web development book and kept running into a brick wall until I found your explanation.

  11. Kevin Solveson on February 3rd, 2008 2:30 am

    Leonard -

    Just wanted to add my thanks for your clear explanation of what the heck is going on with scaffolding in rails 2.0.2. As a newbie, trying to use the “Agile” book (based on 1.1 I guess) but using rails 2.0.2, I was getting pretty frustrated, googling all over the place for some straw of help. I read your post and the lights came on! Thank-you. Sailing along fine now — for the time being :-)

    Kevin

  12. m1k3y2 on February 8th, 2008 2:28 pm

    Thank You very much,
    would you be able to provide more about data types I should use when scaffolding ?

    Thank You,
    M.

  13. Luke S. on February 9th, 2008 5:42 am

    Oh thank jeebus I found this page…I was having all kinds of trouble. Total agile programming/Ruby noob here, I’m coming from mostly a design/ASP/drag-and-drop development background and am delving into RoR to try and be more independent and not rely on other people to build things. It seems it’s advancing faster than the literature can keep up.

  14. samedi on February 9th, 2008 10:49 am

    From my understanding this is not the same. In Rails 1.x you could modify your model and all was immediately updated with dynamic scaffolding. Now you would have to rerun the static scaffold and thus destroy the complete model. From my point of view it´s no more as agile as it was before. Any opinions?

  15. Leonardo Borges on February 9th, 2008 5:38 pm

    samedi,
    You won’t deliver applications with dinamic scaffolds anyway… they were there just to help the first steps. However, people often had to fall back to static scaffolding so they could have more control over the components that make it up. Anyway, firing two small commands to regenerate your scaffolds are not that bad! ;)

  16. samedi on February 9th, 2008 6:46 pm

    Leonardo,

    the problem I see is that these 2 commands regenerate ALL from scratch. That is you should have a more or less clear view about the data fields when starting the scaffold. Otherwise you would have to update the controller manually for each appended field. What I would like to have is a generator to generate a controller based on an existing model.

    Maybe I understand something wrong, but the current static scaffold is not the same as the recent scaffold generator.

  17. Daid Hall on February 14th, 2008 8:52 am

    Thanks. This is obviously a very useful post. I do have the same question as Samedi though. What happens if want to add columns to an existing model. Does this mean I now have to manually update the layouts to add columns etc?

  18. Leonardo Borges on February 14th, 2008 10:17 am

    Daid,
    Yes, it means you have to update them manually. Which is not a problem and it is the way it should be. The old dynamic scaffold feature didn’t have a good performance at all. Everything gets regenerated at each request.

    For development it might be ok, but you will hardly ever develop a rails application where you will need only dynamic scaffolds. That is my opinion anyway… ;)

  19. Never Clever » Blog Archive » Getting Started on Rails, Again on February 23rd, 2008 1:04 pm

    […] really stupid or the tutorial linked above dances over some bits that need doing. Moving on to this tutorial since it seems to address this problem. If nothing else, I learned that I could do the Model & […]

  20. Tom Clancy on February 23rd, 2008 1:08 pm

    Thanks so much for this.

  21. JFinger on February 26th, 2008 10:12 pm

    Thank you!! Much appreciated!

  22. shekhar on February 28th, 2008 7:16 am

    thanks u very much it helped a lot

  23. jason on March 10th, 2008 2:26 pm

    Leonardo,

    This was very useful. Do you know of any of the “how-to” book that adequately teached Rails 2.0.2? It seems all the ones I find are for version 1.2.x and its just a drag!

    Thanks

    Jason

  24. Leonardo Borges on March 10th, 2008 5:09 pm

    Hi Jason,
    Thanks for dropping by.
    The book The Rails Way covers 2.0 and have been quite successful lately.

    Hope this helps

  25. Richard Simões on March 17th, 2008 11:16 pm

    The Rails Way isn’t comparable to AWDR. The former is a reference book for experience RoR developers. It’s not meant to be used as an introduction to RoR. As of yet there are no introductory books using Rails 2.0.x. Let Google light your way!

  26. Leonardo Borges on March 25th, 2008 2:29 pm

    Yes, google is always a good option! :)

  27. Firestoke on March 26th, 2008 8:00 am

    Thanks a lot!!
    I am just starting to learn ruby & rails.
    But I am really mixed up by the different version of rails. Because I am using rails 2.0.2 now, but the video demo, books…etc. They all are old versions. Without your shortly description of their differences, maybe I am still struggling in the deeeeeeep hell………Orz

    Thanks again!
    Firestoke

  28. Ruby On Rails 2.0.x - Scaffolding in der neuen Version | blog.oneandahalf.net on March 27th, 2008 5:21 pm

    […] Rails 2.0: Scaffold […]

  29. Bruno Dumeny on March 27th, 2008 5:59 pm

    it doesn’t work!! :(
    On 2.O.2 version, I’ve always :

    Another migration is already named create_contacts: db/migrate/001_create_contacts.rb

    after the scaffolding.
    why?

  30. Bruno Dumeny on April 2nd, 2008 6:09 pm

    I didn’t understand that I hadn’t to generate model…
    Now it works.

    Sorry!

    Thanks a lot.

  31. christian on April 8th, 2008 12:34 pm

    $ script/generate scaffold Contact name:string email:string

    spent the whole day, now i understand it, thx! but is it possible to get a different model and controller name? for example controllername = “addressbook” and model = “contact”

  32. Leonardo Borges on April 8th, 2008 9:34 pm

    Hey Christian,
    As far as I know, you can’t do that. Seems to have something to do with RESTful resources.. but I’m not 100% sure.

    You can try generating the default scaffold and renaming the controller. Never done it myself.

  33. christian on April 10th, 2008 6:47 am

    thanks leonardo,
    to rename was also the first idea i had, but it didn’t work… no matter

  34. jake on April 27th, 2008 3:44 pm

    Thanks a ton. This is bookmarked, so I can send it to other people. The best and most to-the-point explanation I have come across.

  35. Leonardo Borges on April 27th, 2008 3:56 pm

    Jake, you are more than welcome. I’m glad the post helped you.

  36. nimbletoad blog » Blog Archive » Ruby on Rails 2.0 - Scaffolding is Gone!!! on May 9th, 2008 11:27 pm

    […] hats are off to Leonardo Borges for saving us from Rails 2.0 frustration, or worse - starting from […]

  37. Uncle DIn on May 16th, 2008 4:16 am

    –skip-migration

  38. Sal B on May 16th, 2008 6:37 pm

    Thanks for this post. Been watching the lynda.com essential training and it’s been quite difficult sense the videos are geared for previous rails. Scaffold made my mind hurt till this post - thank you again.

  39. pdxrod on May 20th, 2008 6:54 pm

    Thanks. To get the Depot App (page 67 of Agile Web Development with Rails 2nd edition) to work, this is what you need to do:
    It says write admin_controller.rb like this:
    class AdminController < ApplicationController
    scaffold :product
    end
    This doesn’t work in Rails 2
    Instead, install ActiveScaffold:
    ./script/plugin install http://activescaffold.googlecode.com/svn/tags/active_scaffold
    ./script/generate controller admin
    and make admin_controller.rb
    class AdminController < ApplicationController
    active_scaffold :product
    end
    ./script/server &
    Goto http://localhost:3000/admin
    and see the products listed, just like it says in AWDR page 68

  40. cocolong on May 25th, 2008 1:04 am

    Thanks for this post.

Leave a Reply