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”
Leave a Reply


“Destroy yout model and controller”
Do you just delete the files? i’m thinking of starting over from scratch…
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!
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.
Hey Pete, thanks for the compliments. I’m happy this article helped you.
Regards
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.
[…] Rails 2.0 blog post […]
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
[…] 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 […]
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.
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.
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
Thank You very much,
would you be able to provide more about data types I should use when scaffolding ?
Thank You,
M.
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.
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?
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!
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.
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?
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…
[…] 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 & […]
Thanks so much for this.
Thank you!! Much appreciated!
thanks u very much it helped a lot
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
Hi Jason,
Thanks for dropping by.
The book The Rails Way covers 2.0 and have been quite successful lately.
Hope this helps
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!
Yes, google is always a good option!
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
[…] Rails 2.0: Scaffold […]
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?
I didn’t understand that I hadn’t to generate model…
Now it works.
Sorry!
Thanks a lot.
$ 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”
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.
thanks leonardo,
to rename was also the first idea i had, but it didn’t work… no matter
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.
Jake, you are more than welcome. I’m glad the post helped you.
[…] hats are off to Leonardo Borges for saving us from Rails 2.0 frustration, or worse - starting from […]
–skip-migration
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.
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
Thanks for this post.