Scaffolding w/ Rails 2.0 - Is there a way to do the models first?

I was playing around with the new scaffold generator and I noticed
that if I generated a model first (which creates a migration) and
updated the migration for the model… and then tried to do a scaffold
of the model, the generator won’t create any usable views or
controller. Essentially, it’s not reading the model or database when
creating the scaffold.

But if delete the model and the migration… and run the scaffold
again w/ the data structure of the model specified, it will create a
usable controller and views.

My questions is: Is there a way to create the models first and
then create the scaffolds… now that the old scaffold generator has
been removed?

~ mel

Hi Mel,
I’m not really a rails pro, but from my understanding, if you create a
new scaffold, the rails generators will automatically create a
controller, model and scaffold/view.
Fairly good in my view, but it does take some getting used to.
For Example…

rails testing
cd testing
ruby script/generate scaffold MODELNAME

(Of course replacing MODELNAME with a new model name (that hasn’t yet
been created) like Products)
This will create these files:
exists app/models/
exists app/controllers/
exists app/helpers/
create app/views/products
exists app/views/layouts/
exists test/functional/
exists test/unit/
create app/views/products/index.html.erb
create app/views/products/show.html.erb
create app/views/products/new.html.erb
create app/views/products/edit.html.erb
create app/views/layouts/products.html.erb
create public/stylesheets/scaffold.css
dependency model
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/product.rb
create test/unit/product_test.rb
create test/fixtures/products.yml
create db/migrate
create db/migrate/001_create_products.rb
create app/controllers/products_controller.rb
create test/functional/products_controller_test.rb
create app/helpers/products_helper.rb
route map.resources :products

Hope this answers your question…

AFAIK, the scaffold generator will only generate the views based on
what you provide it at the command line,

If you’ve already created your model, don’t sweat! just add the field
names and types when you generate the scaffold, choose No when it asks
to overwrite your model migration and fixtures. It will generate the
views based on what you provided it.

ruby script/generate scaffold name:string description:text

would create the views with f.text_field(:name),
f.text_area(:description) and (i wish)
f.collection_select(:category_id, @categories, :id, :name)

not that DRY a solution, but better than writing them yourself. Look
into make_resourceful by hampton caitlin, which rocks as a replacement
RESTful scaffold generator.


Thanks Joe. Good suggestion on just specifying the variables again and
it wouldn’t be that big a deal except they are specified differently
then in the migration… which means I have to do more than just copy/
pasting. It is better than creating it all by hand but hopeful they
address this.

I will check out make_resourceful. Hope that will address the issue.


~ mel