Learning Java Web Programming… Again (urghhhh!) Updating the Blog
Jan 23

Rails rules!There is a new model for using scaffold in Rails 2.0. I had no idea that changed (although I did have to change lots of lines in my applications thanks to me not paying attention to deprecation messages from Mongrel.)

It took me some time to adjust to the changes, but now that I saw it, and learned it, it makes sense.

In the old days, when I needed a User database with say, name, email, and password, we would do first the model:

~> ruby script/generate model User

The second step is to edit the migration file like so:

class CreateUsers < ActiveRecord::Migration
# The User class is the doctors who use the system
def self.up
create_table :users do |t|
t.column :name, :string
t.column :password, :string
t.column :email, :string
end
end

def self.down
drop_table :users
end
end

The third step was to run the migration;

~> rake db:migrate

Finally, the last part was to run the scaffold code:

~> ruby script/generate scaffold User

That was the last step. By running the web server you could see your fast application all CRUD included. It was fast and fine, and got a ton of work done with little code.

So it took me a little by surprise that the Rails team decided to do away with scaffold, at least the old model, for a new one. It also made me Google for the new way to do things, but I was very please to try it out.

The new way involves knowing the structure of your model at the scaffold time, which I think it is a very simple requisite, since the model is the first thing everyone plans ahead of time. If we follow the sample above, the code would be

~> ruby script/generate scaffold User user:string email:string password:string
~> rake db:migrate

And that is all! The model and migration are constructed, along with the controller and all the necessary functions inside. It’s definitively easier than the previous way, and although it takes some time to get used to it, once you get the hang, it makes all the sense in the world.

[?]
Share This

written by Administrator

2 Responses to “The New Way To Scaffold in Rails 2.0”

  1. Rido Says:

    so this way when browsing to http://localhost:3000/users it will have all whole CRUD, which obviously should only appear in something like this.

    How can I separate the views like:

    http://localhost:3000/users &
    http://localhost:3000/admin ?

    T.I.A.

  2. Administrator Says:

    Dear Rido:

    It’s a good question, but I don’t see if it’s so obvious. I would probably want a whole CRUD, but if I wanted to separate views for /users and /admin I would just define two different methods in the controller (but using the same model).

    If I understand correctly, you want the view to return a separate page for admin. The way to do it is MUCH better explained in the excellent article by Sonjaya Tandon on login with RoR in the following URL: http://sonjayatandon.com/05-2006/how-to-build-a-secured-web-application-with-ruby-on-rails/#comment-10277

    Look for the end of the article where he builds special view for the admin in a list of users using the same User table and model.

    Good luck!

Leave a Reply