Spring 2.5: Dependency Injection that doesn’t hurt Rails 2.0: XML data type and DB2
Dec 21

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. :)

Related Posts

66 Responses to “Rails 2.0: Scaffold”

  1. Great work Leonardo. Little advice but big help.

    Thanks

    http://www.absolutinvandrare.com

  2. comicprophet says:

    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!!

  3. [...] the reason for this post is the last comment on my Rails 2.0 scaffold post, quoted [...]

  4. comicprophet says:

    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

  5. Thanks for the addition Eric! :D

  6. Alfredo says:

    Can you go in detail on how to add column?

  7. 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.

  8. Alfredo says:

    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.

  9. 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.

  10. Alfredo says:

    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

  11. Nicolas says:

    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.

  12. Alfredo says:

    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.

  13. Alfredo says:

    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)

  14. Alex Costa says:

    thanks for the clue! only then I am able to continue following the “Pragmatic Programmers’ agile development with rails” properly.

  15. Sarma Kolluru says:

    Hi,
    I am a newbe to RoR. I found this is a really really useful tip. Thanks a lot.

  16. Glad to have helped :)

Leave a Reply

preload preload preload