Carl Edwards
Carl Edwards

Reputation: 14434

Creating Methods to an existing Controller via Terminal

I have an existing model that I'd like to add a number of methods to. While I could manually add the methods to the controller as well as its corresponding style/script templates and routes I'd much rather have them done just as I would've when I created the controller in the first place:

rails generate ControllerName hello goodbye

Is there any way to accomplish this?

Upvotes: 0

Views: 127

Answers (2)

user740584
user740584

Reputation:

I think this is a perfectly valid thing to want to do. However the general concensus seems to be that it is too trivial hence why there is no option for it.

A couple of things you could do though:

If you are only adding one or two actions to an otherwise complex controller you could generate another dummy controller with just those two actions. Then copy over the new bits to your existing controller and trash the dummy one. Something like git is great for keeping a track on what the generator changed here.

Alternatively if you want to add a lot of new actions to a trivial controller you could save off the original files, let the generator overwrite the existing controller and then put back the original bits.

Remember you can always use the --pretend option on the generator to see what it would create without actually doing it. This can be useful especially to see what view files it would create for the actions in your case.

Upvotes: 1

Pavan
Pavan

Reputation: 33542

You can do

rails generate controller mycontroller hello goodbye

This would generate controller called mycontroller with methods hello and goodbye.

Upvotes: 0

Related Questions