basic Rails 2.0 scaffold and commands question

Sorry if this a supper newb question... I'm trying to figure out how when I write:

      ruby script/generate scaffold post name:string description:text

this generates model, controller, views, etc... But when I try to back into a table I already have and write:

       ruby script/generate scaffold project

I get no controller. ??? Then if I run:

      ruby script/generate controller projects

I get a empty controller. What is the right series of commands to get a "scaffold'd" controller when you already have a table?

Thanks, Kelly Greer change nospam to yahoo

um..... no reason that shouldn't work unless you already have a file named projects_controller.rb in the app/controller directory

Nope. no file there. Is there another file somewhere that tells it not to do the controller? something else I should delete and then rerun?

Is there a switch to force the standard actions into a "generate controller"ed controller?

Thanks, Kelly

Ok. I have made it a little farther. If you delete the migrations related to the model it recreates the controller. But with scaffolding I now end up with code like this in my index view:

<h1>Listing projects</h1> <table>   <tr> </tr> <% for project in @projects %>   <tr>     <td><%= link_to 'Show', project %></td>     <td><%= link_to 'Edit', edit_project_path(project) %></td>     <td><%= link_to 'Destroy', project, :confirm => 'Are you sure?', :method => :delete %></td>   </tr> <% end %> </table> <br /> <%= link_to 'New project', new_project_path %>

No fields in the output... So, when does Rails 2.0 do database introspection? or does it? where does it store this info? Is there a command to force rails to get its info from a pre-existing table?

Thanks, Kelly

You can append --skip-migration when you're generating a scaffold for an existing table.

There isn't really any 'introspection' being done with the generator. You simply get the fields/references that you pass to scaffold. If you don't pass any... you don't get any in the view(s).

Note that the whole idea of a scaffold is something that is intended to simply be a placeholder that you'll remove later (where "removal" may mean modification and fine tuning). With that in mind you're probably going spend a lot more time trying to trick scaffolding to do what you're after than it's worth. For these tables I'd suggest hand writing (or copy&paste) the markup and moving on.

Am I right in thinking this worked a little different previous to Rails 2.0?

I have a project coming up where the model needs to have 35 to 40 fields. Do I really need to append all these fields and types onto the end of the scaffold command? ouch!


Sort of... Yes, Rails 1.x had a 'scaffold' command that would dynamically render a form like the one you'd get when you ran script/ generate scaffold. But I'd also say "No" in the sense that scaffolded forms were never intended for production (which is probably why the command was deprecated).

A model with 35-40 fields sounds like a model desperately in need of refactoring. A good model should have most of its fields in use by most of it's instances. A model with 35-40 sounds like one that has several sets of fields that are generally used together in certain situations (but rarely all at once). I'd suggest that you could probably break that down into one main model with a number of has_one/ belongs_to relationships. Armed with something like that you can create partials that are included as needed as well.

BTW, the scaffold _command_ was deprecated but the script/generate scaffold was not. If you're creating a model/db-table from scratch then you can still do something like this:

script/generate scaffold MyClass attr1:type1 attr2:type2 attr3:type3...

That will give you the forms that you're looking for.

a few quick follow ups: 1) what is the proper way then to use the "script/generate scaffold" if you are backing in from the database side? or doing "database first"? maybe you are replacing a legacy web interface, but the database/data already exist.

2) what is the correct command to use if you would just like the Controller to be codegen'd (include all the standard methods) and not blank?