Custom Rails generator – note to self

A quick reminder on how to use a custom generator to create a controller

$ rails g applist_controller NameOfModel

Which will create app/controllers/name_of_models_controller.rb.

But remembering that this will probably prompt to overwrite any controller that was created using a scaffold command. The generator will be found in the file, lib/generators/rails/applist_controller/applist_controller_generator.rb; they’re quite fiddly to write.

The scaffold command can create model and view files (using the contents of lib/templates/erb/scaffold. Using a custom scaffold controller (in lib/rails/scaffold_controller/controller.rb) doesn’t work. Maybe there are other ways to use this with the scaffold (c.f., http://guides.rubyonrails.org/generators.html).

Update: A helpful page has suggested that the controller template is saved in lib/generators/rails/scaffold_controller/scaffold_controller_generator.rb. (Obvious when you think about it, really); they’re no easier to write, though.

A few more tweaks are needed once installed but it does 90% of the work with 10% of the effort so well worth the time spent getting it right.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s