Hi, welcome to Word of Mike, my little corner of the internet. I am a Software/Web Developer working in North Yorkshire. I mainly write about programming but my other passion is politics so beware. (click to hide)

2013-01-23 11:24:46 UTC

Only Generate Stubbed-out Controller with Scaffold


Encountered a situation today where I had to create a bunch of controllers to provide admin controls for models that formerly weren't accessible in a CRUD way. I wanted to use scaffolding for the controller as it would save me a whole lot of time and ensure consistency, but didn't want ANYTHING else — just the stubbed out controller.

generate scaffold
gives me a whole lot more and
generate controller
doesn't stub it out. After reading up on the guides, it was obvious that there were config settings you can put in your application.rb to change the default behaviour of scaffold. What I wanted was a one-off way, something I could pass into the command line.

The way that scaffold works is that it basically invokes a bunch of other generators. One of those generators is scaffold_controller which creates creates the controller file then subsequently invokes the template engine, test framework, and helper generators which create all the gubbins that I didn't want. If you run

rails g scaffold_controller -h
you get description of the options available, and after a bit of Googling I managed to infer that you can put "no" before any of the options to eliminate it entirely. My final line, to create one controller file fully stubbed out is:

rails g scaffold_controller admin/agency --no-template-engine --no-test-framework --no-helper
    create app/controllers/admin/agencies_controller.rb