Writing my own generators

I’m failing at the first step here. I’ve tried taking an existing generator and placing it in my .rails/generators folder. I’ve tried using code downloaded from the Rail site - tabbed navbar generator - and placing it in the same directory. I’ve placed both in the RAILSROOT/lib/generators folder. I’ve tried different generators (like CRUDGenerator2)

All of them do the same when I run script/generate: nothing. Nothing at all.

If I call an unknown generator, generate tells me. If I run with trace I still get nothing.

In each case, under the generators/ folder I’ve placed a folder called xxx_generator, containing the templates directory and the ruby file.

I’m running Windows XP.

What am I missing? This happened when I tried to write generators before, on a different PC.


After a bit of head-bashing I finally managed to solve this one. For
the sanity of those who come after me, I'm putting it here.

There were two problems:

1: Although script/generator will recognise generators placed in a
folder called 'foo_generator' in one of the many generator directories
(~<user>/.rails/generators or RAILSROOT/lib/generators or more), there
is a problem. You can call 'script/generate foo' or 'script/generate
foo_generator' and generate won't generate an error.

At least, with 2 versions of Rails on Windows.

In desperation I did a 'rake rails:freeze:edge' on my test project, and
happily I started getting error feedback.

From there it was obvious. Generate was trying in vain to find a file

called foo_generator_generator.rb - a classic case of some code being
too clever for the rest of the code.

Changing the name of the folder was... impopssible. As was deleting it.
Looks like Rails puts its hooks into a generator and doesn't let go - I
was told the file was in use, but windows never tells you who's using

I copied it and called the copy foo... Which then confused Generate
mightily. It couldn't decide between the different foos, and told me

Making ANOTHER copy and calling it 'bar' did the trick.


Why can't I write a generator can't write a generator my generator
doesn't work generator not working script/generate no errors scaffold
scaffolding rails generator instructions


I may update the wiki...

Ben Dunkley wrote: