Skip to content

Commit

Permalink
Don't assume fetchOptions has a body (#384)
Browse files Browse the repository at this point in the history
The event handler in encodeMethodIntoRequestBody was failing when forms
were submitted using `method="GET"`, because it assumed the presence of
`fetchOptions.body`, and such forms don't have a body.

Checking for the presence of `body` before accessing it avoids the
crash, and the resulting console noise.
  • Loading branch information
kevinmcconnell authored Sep 14, 2022
1 parent 50fb92f commit fb519d5
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/assets/javascripts/turbo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3936,7 +3936,7 @@ function encodeMethodIntoRequestBody(event) {
if (event.target instanceof HTMLFormElement) {
const {target: form, detail: {fetchOptions: fetchOptions}} = event;
form.addEventListener("turbo:submit-start", (({detail: {formSubmission: {submitter: submitter}}}) => {
const method = submitter && submitter.formMethod || fetchOptions.body.get("_method") || form.getAttribute("method");
const method = submitter && submitter.formMethod || fetchOptions.body && fetchOptions.body.get("_method") || form.getAttribute("method");
if (!/get/i.test(method)) {
if (/post/i.test(method)) {
fetchOptions.body.delete("_method");
Expand Down
Loading

0 comments on commit fb519d5

Please sign in to comment.