Delete action in Rails 3
At Carnes Media, we’ve been really happy with using a delete action as a confirmation step before calling destroy.
This solves a bunch of problems:
- No more hacky inline javascripts that create a DELETE form.
- We render the delete form in a modal instead of a confirm dialog.
- It’s a completely unobtrusive solution and easy to test with cucumber.
Here are a couple of bits and pieces that help make this pattern easier:
app/views/shared/_delete.html.erb
This partial can be stashed in a shared view folder and rendered as the delete form for most views. It uses formtastic, but could easily be rewritten without it.
NOTE: For the rendering of actions in modals without the layout getting in the way, you might want to check out another poorly named plugin.
lib/add_delete_to_resource_routes.rb
I know, I’m terrible at naming things.
This little file adds a delete action to the default set of actions for “resources” routes. So when you put resources :widgets in your routes.rb file, a delete action (GET) will be added to your routes along with index, new, create, destroy, etc…
Just put this in lib and require it in your Application class in config/application.rb.
If there is demand, I might work this into an actual plugin…
