script/generate scaffold Post title:string body:text category_id:integer
Read my books:
Ruby in Practice (http://manning.com/mcanally/)
My free Ruby e-book (http://humblelittlerubybook.com/)
Or, my blogs:
Roy Pardee wrote:
AFAIK, the short answer to your first question is no. You'll need to
specify the fields on the command line when you generate the scaffold. The
answer to your second question, I think, is no. You're not out of luck.
You can specify some of the fields on the command line when you generate the
scaffold, edit the migration to add the remainder of the fields, and then
run the migration. You'll have to edit the new/edit/show views to add the
new fields, but if you choose the ones you specify on the command line so
that you've got one of each type that exists in the table you're modeling,
then it's pretty much a cut-n-paste. So if you'd be happy with...
- generate the scaffold
- edit the migration (specifing the additional fields, field lengths,
- run the migration
- edit the new/edit/show views (adding the fields you decided not to
then you're in luck.
I'll second that. I'm putting together a little Rails presentation with
demo for work. To help things along I decided to do a little work up
front on my own scaffold generator. Based on the existing one I created
my own which creates layouts and styles with the same look & feel as our
existing web apps. Five commands in the shell, switch to the browser
and there's something familiar to look at already.
Posted via http://www.ruby-forum.com/.
The existing generator is in:
(depending on your version of Rails - run "gem environment" to see where
your gems are).
Rails will look for generators in ~/.rails (on UNIX/MacOS - don't know
about Windows) so if you wanted your generator to be called "funky":
mkdir -p ~/.rails/generators/funky
mv scaffold_generator.rb funky_generator.rb
Then edit funky_generator.rb and replace the class name
ScaffoldGenerator with FunkyGenerator and also update the "banner"
Now, in any Rails application you can use:
script/generate funky MyModel field1:type1 field2:type2 ...
and get the same results as the standard scaffold.
Now, update the USAGE file to be more relevant to your scaffolding and
update the contents of the template directory accordingly.
Note that the template files are run through ERb when scaffolding
happens, so ERb commands that need to remain will start with "<%%".
That is, where you see "<%%", this will get passed through as "<%" and
where you see "<%", this will get evaluated and the result passed
Any processing you want to do for creating the scaffold and any files
you want/don't want to create are specified in funky_generator.rb.
To make the generator available to all users, turn it into a gem and
install it in [GEM_HOME].
The model might not already exist, but you could create one locally on
the fly in the scaffolder and reflect on that. The class method
#column_names provides you with a list of column names.
> It's probably insane of me, but scaffold_generator.rb doesn't actually
> look that that daunting
No, it's fairly straightforward. Look at:
for the parent class of the scaffolder to see more of what's going on.
It's easier than it sounds. And if anyone intends creating a number of
applications with the same look and feel then it's definitely worthwhile
creating your own scaffolder, or at least your own generator which can
create the structure, stylesheets and layouts you want.
Have you checked the svn repository for the Rails trunk?