#58
Jul 16, 2007

How to Make a Generator

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.
Tags: tools
Download (33.1 MB, 11:11)
alternative download for iPod & Apple TV (16.9 MB, 11:11)

Resources

# app_layout_generator.rb (see link above for full source)
class AppLayoutGenerator < Rails::Generator::NamedBase
  def manifest
    record do |m|
      m.template "layout.rhtml",   "app/views/layouts/#{file_name}.rhtml"
      m.file     "stylesheet.css", "public/stylesheets/#{file_name}.css"
    end
  end
end

28 comments

Nico Orellana Jul 16, 2007 at 00:52

This is a great way to contribute to the rails community, In Chile we were talking lately about how to give something to the community for all the things we get from them. Thanks, we have another way.


maze Jul 16, 2007 at 01:41

another great railscast! thanks a lot and keep up the good work!


chineseGuy Jul 16, 2007 at 02:08

great!


João Victor Jul 16, 2007 at 09:19

Great!!!


Poor Windoze User Jul 16, 2007 at 09:34

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?


Poor Windoze User Jul 16, 2007 at 10:08

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 :)


Brent Beardsley Jul 16, 2007 at 10:10

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?


Dr Nic Jul 16, 2007 at 10:15

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.


Ryan Bates Jul 16, 2007 at 10:33

@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/


InMan Jul 16, 2007 at 11:18

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..


Ryan Bates Jul 16, 2007 at 13:07

@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.


InMan Jul 16, 2007 at 14:23

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 .


David McNally Jul 17, 2007 at 11:50

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!


Dr Nic Jul 17, 2007 at 13:25

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.


rudy Jul 18, 2007 at 16:40

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?


Ryan Bates Jul 18, 2007 at 17:15

@rudy, does your app layout generator have a "manifest" method?


Bryce Jul 19, 2007 at 17:06

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


@rudy Jul 19, 2007 at 18:03

manifest not mainfest, derrr. Creating this generator is the sickest thing I've done yet with rails. Thanks Ryan. Railscasts.com is the bomb!!!!!!!


Ed Jul 27, 2007 at 01:09

Always wanted to do something like this with generator. This screencast is great. Thanks Ryan.


infrid Aug 01, 2007 at 04:45

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.


hhifnawi Aug 21, 2007 at 19:26

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


Ryan Bates Aug 22, 2007 at 08:08

@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.


Dr Nic Aug 25, 2007 at 07:41

For anyone interested in Generators for other development areas outside of Rails, check out http://rubigen.rubyforge.org


Ben Sep 30, 2007 at 02:29

Thanks so much Ryan!

Only constructive-criticism that I have regards the down-time in your screencasts. In this episode in particular, there were a couple REALLY long pauses when nothing complex was happening, and then no pauses when a couple command line programs were run in rapid succession. More consistent, medium-length pauses would be my only suggestion.

Thanks again though, I'm REALLY appreciating these screencasts =).

--Ben


Ben Sep 30, 2007 at 06:39

Disregard my above comment. I didn't have the right codec and thus your movies had no sound. I just assumed this was intentional.

I'm kind of shocked that I was totally able to follow along without sound. That's a testament to the clarity of both your screencasts and ruby!

--Ben


Valery Apr 18, 2008 at 06:17

Very useful episode. Thanks lot.


kino May 23, 2008 at 01:52

By means of analysis, our synthetic judgements have lying before them the noumena.


Eric Berry Jul 09, 2008 at 13:34

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?

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(required)

subscribe:
sponsored by:
if you want to help:
required:
Get Quicktime Player