How do you send a request with the “DELETE” HTTP verb?

I'd like to create a link in a view within a Rails application that does this...

DELETE /sessions

How would I do that.

Added complication:

The "session" resource has no model because it represents a user login session. CREATE means the user logs in, DESTROY means logs out.

That's why there's no ID param in the URI.

I'm trying to implement a "log out" link in the UI.

Answers


Correct, browsers don't actually support sending delete requests. The accepted convention of many web frameworks is to send a _method parameter set to 'DELETE', and use a POST request.

Here's an example in Rails:

<%= link_to 'log out', session_path, :method => :delete %>

You may want to have a look at Restful Authentication.


I don't know about Rails specifically, but I frequently build web pages which send DELETE (and PUT) requests, using Javascript. I just use XmlHttpRequest objects to send the request.

For example, if you use jQuery:

have a link that looks like this:

<a class="delete" href="/path/to/my/resource">delete</a>

And run this Javascript:

$(function(){
    $('a.delete').click(function(){
        $.ajax(
            {
                url: this.getAttribute('href'),
                type: 'DELETE',
                async: false,
                complete: function(response, status) {
                    if (status == 'success')
                        alert('success!')
                    else
                        alert('Error: the service responded with: ' + response.status + '\n' + response.responseText)
                }
            }
        )
        return false
    })
})

I wrote this example mostly from memory, but I'm pretty sure it'll work....


Correct me if I'm wrong, but I believe you can only send POST and GET requests with a browser (in HTML).


Rails' built in method for links will generate something like this:

<a href="/sessions?method=delete" rel="nofollow">Logout</a>

If you don't want to use the Rails' built in method (i.e. don't want the rel="nofollow", which prevents search engine crawlers from following the link), you can also manually write the link and add the data-method attribute, like so:

<a href="/sessions" data-method="delete">Logout</a>

Browsers can only send GET/POST requests, so this will send a normal GET request to your Rails server. Rails will interpret and route this as a DESTROY/DELETE request, and calls the appropriate action.


Need Your Help

Jquery Validation Any one of the fields mandatory

jquery jquery-validate

Using the Jquery validation plugin, I was curious to know if there is some sort of in-built functionality to have a "one of the two" is mandatory check. e.g: Email or mobile number is mandatory?

Getting textbox value from one page to another page textbox value

asp.net html forms

I am creating a form in my website and i link this form to the google docs (FORM which i created there). But this new form that i am trying to do is this in such a way that a textbox value from the...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.