Programming Phoenix 1.4: Chapter 5 HTTP DELETE clarification (page 98)

I’ve been dabbling in Elixir for years, but I’m finally learning Phoenix properly. Very much enjoying this book and how it explains everything the framework is doing along the way to prove there is no magic.

However, I think I found some magic (or at least, some behaviour that does not follow the explanation) for the link helper.

Quote:

The link:

• Uses the HTTP delete method

By passing the :method option to link, Phoenix generates a form tag instead of an anchor tag. Links without a specified HTTP method will default to GET, and Phoenix will render a simple link.

However, when I inspect the HTML in the browser, it has generated an anchor tag

<a data-csrf="NhNzCRlHNh4vGSxzPjAqKj1EBiZiNXMoig59otnzWXVGFCGsyivk1vEo" data-method="delete" data-to="/sessions/6" href="/sessions/6" rel="nofollow">Log out</a>

When I read this, I expected a <form> tag instead of an <a> tag.

I suppose the anchor tag is a kind of form tag, but it’s not a <form> and the “instead of an anchor tag” is confusing. Maybe it meant instead of an <a href> tag?

When clicked, the request is submitted as form data using post, not delete, contrary to the explanation. Although the delete action on the controller is called, so somewhere something must be interpreting post requests and checking for a _method paramter to route it as if the _method was the HTTP method.

1 Like