ApplicationController needs a #delete method

I’ve just finished a toy project that I call “righteously ubiquitous
javascript”:

https://github.com/rdpoor/righteous_ujs

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:

#new => #create
#edit => #update
#delete => #destroy

Make sense? Is DHH listening? :slight_smile:

Peace out.

  • ff

You can easily add a delete method to your controller:

def delete
@post = Post.find(params[:id])
end

Then add it to your routes as well:

resources :posts do
get ‘delete’, :on => :member
end

And you’re done

Tim S. wrote in post #1038479:

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 ActionController::Base?

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.

Tim S. wrote in post #1038731:

I apologize… I guess I wasn’t sure what you were asking.

No problem!

ActionController::Base doesn’t have any methods included.

You’re right – I mis-spoke:

s/“ActionController::Base”/“standard Rails distribution”/g

In addition to the resourceful routes, you’d need extensions to the
various generators and templates, but yep, that’s what I’m arguing for.

I can definitely see your case for wanting to do this.

Upon reflection, the right thing to do would be to open a lighthouse
ticket and let the pros decide. Wish me luck! :slight_smile:

  • ff

Alexey M. wrote in post #1038755:

I like the idea.

By the way, the issues are not on Lighthouse anymore but on GitHub:
Issues · rails/rails · GitHub

Right you are. So, feeling a little like David facing off against
Goliath, I’ve submitted a ticket:

RESTful frameworks should have a delete path and #delete controller method · Issue #4234 · rails/rails · GitHub

I hope they go gentle on me!

  • ff

I guess FF’s question was about resourceful routing, like in routing
with

resources :events

I like the idea.

By the way, the issues are not on Lighthouse anymore but on GitHub:

Alexey.