and the experience has convinced me that Rails controllers need a
#delete method that parallels the contracts of the #new and #edit
methods. To explain: The three "verbs" that cause a change to model
state are:
CREATE a model instance
MODIFY a model instance
DELETE a model instance
For each verb, the controller needs to implement TWO methods. The first
method fields the request from the user to initiate the action, to which
the controller responds by rendering a GUI element that allows the user
to complete the command. The second method is the result of submitting
the GUI element.
That's actually the contract of #new and #edit:
#new => render a GUI element that calls #create when submitted
#edit => render a GUI element that calls #update when submitted
The problem is with DELETE (aka #destroy). It doesn't follow this
pattern, but it should: the controller lacks a method that renders a GUI
element. It OUGHT to be:
#delete => render a GUI element that calls #destroy when submitted
Absent a #delete method, the Rails ends up relying on Javascript to
generate the GUI element that calls #destroy, which is Not Nice in a
system that claims to implement ubiquitous javascript.
This could easily be fixed by adding a #delete method to
ApplicationController which parallels #new and #edit. It would avoid
the issue about requiring Javascript, and as a side effect, would make
it really easy for newcomers to understand the relationship between:
You can easily add a delete method to your controller:
... which is exactly what I did in the cited code example.
I'm not sure how to interpret your reply. Sure it's trivial to add.
Are you claiming that a delete method should NOT become part of the
standard ActiveController?
I apologize… I guess I wasn’t sure what you were asking.
ActionController::Base doesn’t have any methods included. Not even the index, new, edit, etc. The only methods available are the ones you add in your application. So adding delete method to ActionController doesn’t really make sense.
The delete method could be added to the default set of resourceful routes so you wouldn’t need to add it to your routes manually, which is what I think you were referring to? I can definitely see your case for wanting to do this.