Rails, can we please have a delete action by default.

I’ve just finished watching the latest Railscast… thanks Ryan, great as ever! One thing that bothers me is that we are still not able to delete items with the default CRUD setup when javascript is turned off.
Way back in 2006, I wrote an article called Simply RESTful… “The missing action”. I still believe that we should have a “delete” action that responds to GET requests by default in Rails, much like the “update” action has an “edit” form. Now before you say “that’s ridiculous Jamie”, I am not suggesting that we should be able to delete via a get request (that would be stupid!), the “delete” action would simply be a confirmation page with a form that posts to the “destroy” action. That way we can still use javascript to hijack the “delete” link and make it perform a “DELETE” request to the “destroy” action but we also have the benefit of being able to delete things without javascript.
Remember, the “delete” action is to “destroy”, what the “edit” action is to “update”. So in the case of a “products” resource, we would simply render the following form named “delete.html.erb”:

<h1>Delete product</h1>
<p>Are you sure you want to proceed?</p>
<% form_tag product_path(@product), :method => :delete do %>
  <div><%= submit_tag 'Confirm delete' %></div>
<% end %>
<p><%= link_to '« Back', products_path %></p>

What do you think?

Join the conversation


  1. I can understand both sides here. In some ways I would like to see a “delete” action be there by default, but the implementation of the confirmation page would be a lot of duplication across controllers.
    Certainly Rails could handle the confirmation page behind the scenes but I think that kind of implementation fits better in a plugin. I hope with Rails 3’s approach to plugins it will be much nicer to write a plugin which extends “resources” in routes and adds a delete action and view.

  2. This seems like an oversight. UI designers rarely use actual form buttons for something like a delete in a table or grid layout. Javascript is great, but we’re trying to support feature phones or the tinfoil hat crowd too now, right?
    I suppose hand rolling a delete action (pluls adding a route) for only those pages where this type of delete is used makes sense, but when you start to think about apps designed around REST, isn’t nearly every resource a potential candidate?
    I’d love to see a plugin like this. I wouldn’t even know where to start.

  3. @Ryan Bates I will take a look at implementing some kind of engine that adds this functionality, however I still can’t help but think deletion without javascript should be available out of the box.
    Thanks for the great set of screencasts on Rails 3, looking forward to the next instalment.

  4. I completely agree with this method of managing destroy. In fact it’s so obvious that I am surprised it’s not been adopted in mainstream.
    Are there any good reasons against this ? (considering that it’s still up to the developer to take advantage of this at the end of the day).
    The duplication of delete managing code across controllers is really down to implementation. I could easily see me inheriting the delete method and using a common form for displaying the confirmation page (with code to determine the model name etc..).
    And ultimately no one has argued that managing Edit code or create code across controllers is duplication have they ?

  5. I think this is a great thing to have. You could have the route look much like edit /resources/:id/delete. Why isn’t this out of the box? We as a community want ujs so we should work without js.

  6. Yes, I agree too. I have to confess that I’ve mapped a “GET logout” action to do the same as a typical HTTP DELETE action for login sessions as I’ve often been frustrated by the fact pressing the logout button while pressing Ctrl has opened a new tab, but done some other stuff due to the JS not being invoked.
    I do think Ryan does have a point. However, would could be rather nice here as a side effect of programming a full on delete confirmation page, is that you could leverage jQuery (or whatever else) to request this page and extract out the main content via a CSS selector and show it as a JS based confirmation in some modal form, rather than use some ugly onclick=”return confirm(‘Ya sure?’)” like rubbish that the old JS helpers do.

  7. I totally agree! I’d personally take a confirmation page over a js popup anyday.
    I realize that this isn’t necessarily arguing to replace the JS method with a confirmation page… but one of the benefits to a confirmation page that Django utilizes is that it gives you a place to list any related records that could be deleted!
    Also, a plugin/engine would be great! Seems like it should be pretty straightforward.

    1. @Ahmed Very useful, I’d like to see a solution without using alias_method_chain… I may have a crack at this over the next couple of days.

  8. I know this is over a year old… was wondering if anything materialized since. I would love to see this issue get resolved in rails.

    1. @Ivgeni Unfortunately not, not sure why, seems an obvious oversight to me. Perhaps I’ll try and convince people again 🙂

Leave a comment

Your e-mail address will not be published. Required fields are marked *