Jamie Hill
Photo of Jamie Hill

UX Designer, CSS Wizard, Ruby on Rails Developer.

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?