Encode HTTP method into Request body as _method
#370
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Browsers do not handle
<form>
element submissions with a[method]
value other than
get
orpost
. This also applies to<form>
elementssubmitted with submitter elements that declare
[formmethod]
valuesother than
get
orpost
.To circumvent that constraint, Rails form builders encode other verbs
into a
_method
parameter that is forwarded to the server.On the other hand, Turbo's reliance on fetch means that it can
lift
[method]
or[formmethod]
values directly tofetch
calls,regardless of verb.
The way that Rails responds to
DELETE
orPATCH
requests submitted inthat manner differs from those with
_method=DELETE
or_method=PATCH
.Primarily, controllers that call
redirect_to
default to using a 302Found status. When a Turbo request with
PATCH
receives a302
response, the subsequent request to the resource is also made with the
PATCH verb, often resulting in unintended side-effects or unhandled
requests.
The Turbo documentation suggests responding to those requests with 303
See Other instead. This is sound advice, and should be applied.
However, for Rails applications migrating to Turbo from Unobtrusive
JavaScript, making that change application-wide can be demanding.
For reasons outside the scope of the pull request, Rails' internal
machinery redirects requests made with
POST
verbs that encode a_method
parameter in a way that doesn't require a303
.This commit transforms the existing
turbo:submit-start
event listenerinto a
turbo:before-fetch-request
listener that transforms requestswith methods other than
POST
intoPOST
requests that encode theirmethods as
_method
.The net result is that migrating applications can gradually introduce
303 See Other
redirects at their own pace.