Reputation: 2053
Say I have a Loan resource in my app. It can be accepted or rejected by a user. I could add accepted
and rejected
actions to my LoansController
, but is there another option that will keep my controller RESTful?
Upvotes: 2
Views: 264
Reputation: 13952
There's a few approaches.
You could have a LoanAcceptances
controller with a create
and destroy
action - that is, rejecting a loan is "destroying a loan acceptance". That's counter-intuitive, but does keep your controllers "RESTful".
You could have a LoanAcceptances
controller with a create
action and a LoanRejections
controller with a create
action - that is, rejecting a loan is "creating a loan rejection". And now you have two controllers with just a single method.
Or, you could just put them both in LoansController
as accepted
and rejected
. This violates the design principle of "REST" but adheres to common sense.
You'll find people willing to argue to the death for either approach - but don't get too caught up in it. Either will work. If you're finding your LoansController
is getting an ever-increasing proliferation of methods (eg. accept
, reject
, mark_pending
, apply_interest
, make_payment
, etc), then perhaps it's worth pulling them out and going the "separate controllers" approach.
If you're not finding that, it's fine to keep it simple and put them in the same controller.
Some time ago I asked a similar question which you'll find interesting to read: non-RESTful actions in Rails
Upvotes: 1