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.

Ben

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
(~/.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
it.

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

Making ANOTHER copy and calling it ‘bar’ did the trick.

<google_bait>

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

</google_bait>

I may update the wiki…

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs