#58 How to Make a Generator
Jul 16, 2007 | 11 minutes |
Tools
Rails comes with many built-in generators, but what if you want to customize them or make your own? Rails gives you a great way to do this which you will learn in this episode.
- Download:
- mp4Full Size H.264 Video (21.7 MB)
- m4vSmaller H.264 Video (12.7 MB)
- webmFull Size VP8 Video (36.5 MB)
- ogvFull Size Theora Video (30.2 MB)
I'm stuck using Windoze for development and so I tried unzipping your app_layout into C:\documents and settings\myusername\.rails\generators\app_layout and it doesn't find it :(
Any ideas on how to do this in Windoze?
Figured it out. For some reason ruby isn't using \ but is using / and so as soon as I changed USERPROFILE to use / it worked :)
So how would one go about adding their own commands to Manifest so that they would rewind properly and available in the record block so I could do m.mycmd?
Another champion episode.
I've written generators before but I don't think I'd ever thought to use them for simple tasks like this.
So now, I have a database.yml generator to include my dev mysql.sock details, local password etc. So thanks for the inspiration.
@Brent, good question. I recommend checking out the Rails source code to see how this is done. Go through the different ruby scripts at the URL below (particularly base.rb and commands.rb):
http://svn.rubyonrails.org/rails/trunk/railties/lib/rails_generator/
Great episode. That was needed :).
For learning I tried to improve it. Added --no-css option and default file name.
http://pastie.caboo.se/79207
Actually i want to add <title><% yield :title %></title> too. It was on one episode by Ryan before. Maybe someone can throw a link, how to modify some other file..
@InMan, great job! I like what you've done there! It's an excellent example of how to add options to a generator.
You may want to override the "banner" method to provide a short usage description.
http://pastie.caboo.se/79278
Actually attr_accessor isn't needed for views :).
Banner removes just first line actually. (atleast in edge).
But maybe I'm wrong. Haven't tested it yet .
Hi Ryan, I've recently discovered your site and can I just say what a great asset it is. Keep up the good work and apologies in advance for systematically downloading episode after episode!
Note: in edge rails you can now store generators within RubyGems' /generators or /rails_generators folder. Similarly, plugins you can now store generators within /rails_generators as well as the existing /generators folder support.
I'm having a bit of trouble. I'm storing my generator in the plugins dir. C:\projects\cap\vendor\plugins\app_layout\generators\app_layout
I'm getting the following when I try and run ruby script/generate app_layout.
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/rails_generator/base.rb:122:in `manifest': No manifest for 'app_layout' generator. (NotImplementedError)
Any Suggestions?
@rudy, does your app layout generator have a "manifest" method?
This generator cast was great. I'd been waiting for something to be broadcast so clearly regarding generators. Thanks a lot Ryan. For everyone else, I am trying something a little different and ran into some trouble. Would people mind taking a look at my post at http://railsforum.com/viewtopic.php?id=7711 and helping a brother out?
Thanks,
Bryce
manifest not mainfest, derrr. Creating this generator is the sickest thing I've done yet with rails. Thanks Ryan. Railscasts.com is the bomb!!!!!!!
Ryan, thanks so much - I'm glad you're doing 1 a week now (though I'd happily still watch and absorb 3), but it's a helluvalotof work.. If you ever decide to make full length screencasts, I'm sure people would be interested.
great stuff, thanks as always.
NEED help:
My group are working on rails IDE and we're in the wizard part now and we're trying to generate the Rails skeleton and we looked in the rubygems and found in
gems/1.8/gems/rails-1.2.2/lib/rails_generator/generators/applications/app/app_generator.rb
what does it do, does it generates rails skeleton? are there any other scripts we need to execute as well?
plz email me at hisham86@aucegypt.edu
@hhifnawi, this won't generate the full rails skeleton, just a couple files. You should use the "rails" command to generate a full new Rails app. If you still have problems I recommend posting on railsforum.com.
For anyone interested in Generators for other development areas outside of Rails, check out http://rubigen.rubyforge.org
Great screencast! I was wondering how you would package this generator for use by other developers? Would you create a gem out of it, or a plugin somehow?
How can I delete a file in the generator? I'd like to delete the public/index.html if it exists.
And how can I remove all the files which are created with the generator?
hi!
This example was clear !
But how can I pack a generator in order to make it a dist and publish it. I want some way to make distributable, like gems or plugins. How can I do this?
Thank you
The Rails Generators Source Link doesn't work anymore...
Shouldn't this episode be marked as revise? Check out episode 218:
http://railscasts.com/episodes/218-making-generators-in-rails-3