Rails 2.0: Scaffold
Filed Under (Rails, Ruby) by Leonardo Borges on 21-12-2007
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.



“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.
Thanks. I was just getting a bit frustrated with the book “Agile Web Development with Rails” and found this one with Google.
You’re more than welcome.
Leonardo,
Thank you for this example. I just started using the book “Beginning Rails: From Novice to Professional” from Apress, which is based on Rails 1.2.3. I was checking out the book’s website and read the “disclaimer” about some features being changed with Rails 2.0. I am currently using the InstantRails package that ships with Rails 2.0.2, which I would rather learn than Rails 1.2.3. This is a big help for a Rails newb, and will be bookmarked. Thanks again for your assistance.
Always glad to help!
Hi compatriot ;),
Hi made this and everything worked like a charm.
Now I´m changing my model(I´m following AWDWR), I added a field in my database using migrate and rake to perform the changes.
The changes were done in my database, BUT the interface was not updated.
I also added a validates_presence_of and it did´t changed anything in my interface :(.
Do I need to run some command ?
Thanks
Hi!
Whenever you change your model, you need to update the interface (view) either by hand or regenerating the scaffold.
Rails scaffold is really intended only for a starting point and should be updated by custom code as soon as possible.
Let me know if u need some help.
dudes, this site rocks - you are all part of the solution, here is my bit =>
if you script/generate scaffold
then you can safely destroy everything the scaffold created by:
script/destroy scaffold
cheers
Mike
tks for your bit, Mike!
Hey man,
Thanks a lot.I was in gr8 mess before as i cudn`t able to scanfold.But your given help was worth cent percent.Keep it up dude…..
Since I had used reference in my migrations, I wasn’t sure how do I mention that as an argument.
script/generate scaffold Person name:text city:reference age:integer
worked for me in generating a city_id as a foreign reference.
Afterthought: However, when I look at my database, even though it shows city_id in Person table, it does not show it as a foreign key. We’ll figure that out soon.
Great work Leonardo. Little advice but big help.
Thanks
http://www.absolutinvandrare.com
Hi Leonardo,
I am extremely grateful for your info altruism. I thought maybe I could show my gratitude with a small donation but I don’t see anywhere on your site to do so.
You’ve saved me hours and possibly what little sanity that I claim to have left. Is there a place to do so on your site that I’m missing? If not just tell me where and I’ll send my gesture to any cause that you wish. Thanks very much again, some day I hope to be able to be of service to a needy newbie as you have been to me. Happy New Years!!
[...] the reason for this post is the last comment on my Rails 2.0 scaffold post, quoted [...]
Hi Leonardo,
I have found an amazing tutorial that restates the advice that you have given plus gives a completely fleshed out example, so here’s my chance to help point some people in the right direction too.
http://fairleads.blogspot.com/2007/12/rails-20-and-scaffolding-step-by-step.html. Thanks again. Eric
Thanks for the addition Eric!
Can you go in detail on how to add column?
Hello alfredo
Would you mind giving more details on what you need?
This line of code:
script/generate scaffold Contact name:string email:string
Is supposed to add the column for you.
After using this line of code: script/generate scaffold Contact name:string email:string, and you want to add a missing column. I guess my question is the same question that Samedi had about regenerating scaffold.
In this case you have 2 options:
You either recreate the migration or add the missing field by hand - which is by far the most common.
Think of scaffolding as a easy place to get started. You will always need to touch it at some point during your project.
I understand I have to update the scaffold manually. I was wondering if you could explain in detail on how to update the scaffold manually
Well, that works !
But it’s not really easy to use if your table gets 20 ou 30 columns.
scaffold should be able to read 001_create file if exists.
I am currently working on the depot program, and so far it’s working fine, but now I am trying to add a missing column. I have file called 001_create_admins, and know I have a file called 002_create_add_price which I have manually entered the code:
class CreateAddPrices 8, :scale=>2, :default=>0
t.timestamps
end
end
def self.down
remove_column :products, :price
end
end
For some reason rake db:migrate does not recognize this file(002_create_add_price), so when I refresh my browser, the changes are not displayed in my web.
I did exactly what you have in your code, and now there is a message that says:
==2 AddDescriptionToTasks: migrating=====
—add_column(:tasks, :description, :string)
rake aborted!
SQLite3::SQLException: no such table: tasks: ALTER TABLE tasks ADD “description” varchar(255)
thanks for the clue! only then I am able to continue following the “Pragmatic Programmers’ agile development with rails” properly.
Hi,
I am a newbe to RoR. I found this is a really really useful tip. Thanks a lot.
Glad to have helped