method: :delete

March 16, 2012

I’m sure we’ve all seen the following code before:

<%= link_to “Destroy”, my_object, method: :delete %>

minus the new hash calls in 1.9.2, this is a pretty standard delete link rails.  Here’s the issue though, links can’t make actually delete requests so how does this work exactly?

What rails is doing, is on the fly, if you’ve added this method, its actually makes your application execute a javascript function when clicked that then creates a delete request on the server side so that the routes work properly.  The only downside to this is that if your users don’t have javascript running, this links fails epically.  If you have users who don’t use javascript you basically have two options:

Personally, I’d opt for two but the choice is yours.  Either way, you should be aware of what you are presenting to users.

Discussion, links, and tweets

I'm a developer at thoughtbot.

Follow me on Twitter. I rarely post anything but it can't hurt to try.