The older versions of HTML supports only GET
and POST
. The newer version includes PUT
, PATCH
, and DELETE
which are treated as XHR(XMLHttpRequest)
requests or simply AJAX
requests by all major web browsers.
When the requests for PUT or PATCH or DELETE are made, Rails under the hood, with the help of Javascript processes these requests as XHR
requests to the client. When the Javascript in your application is not working or disabled, these requests will fall back to GET
Now coming back to your code snippet which is not working. You have the below
<%= button_to 'Delete', {:controller => :users, :action => 'destroy', :id => user.id}%>
button_to
by default creates a form and sends the request as POST
, unless if you are explicitly overriding it, as you are doing in the first case with :method => :delete
. So the above code snippet generates a url like users/1/
, but as a POST
which eventually fails as you don't have any such route defined in the routes.rb
Shouldn't the url this resolves to be users/destroy/1 ?
No, when you use :method => :delete
in your code, the url that is generated will be users/1
but the request is sent as DELETE
, which eventually works as it is a valid request according to your routes that are defined in routes.rb