inherited_resources icon indicating copy to clipboard operation
inherited_resources copied to clipboard

`Create` & `Update` should return `status code != 200` for failed requests

Open a-nickol opened this issue 1 year ago • 5 comments

To make inherited_resources work with the current version of rails out of the box, it is not allowed to not redirect if status code is 200 on a submitted form.

Since on a failure we want to render either :new or :edit, we are not allowed send status code 200, since this will result in the following error:

Error: Form responses must redirect to another location
    at _FormSubmission.requestSucceededWithResponse (application-c20cb2d408231ec629db20f6bfe840b8b84f28b26acee36de46dd6b684b8aa43.js:1298:24)
    at FetchRequest.receive (application-c20cb2d408231ec629db20f6bfe840b8b84f28b26acee36de46dd6b684b8aa43.js:1068:23)
    at FetchRequest.perform (application-c20cb2d408231ec629db20f6bfe840b8b84f28b26acee36de46dd6b684b8aa43.js:1046:27)
formSubmissionErrored @ application-c20cb2d408231ec629db20f6bfe840b8b84f28b26acee36de46dd6b684b8aa43.js:2854
requestSucceededWithResponse @ application-c20cb2d408231ec629db20f6bfe840b8b84f28b26acee36de46dd6b684b8aa43.js:1299
receive @ application-c20cb2d408231ec629db20f6bfe840b8b84f28b26acee36de46dd6b684b8aa43.js:1068
perform @ application-c20cb2d408231ec629db20f6bfe840b8b84f28b26acee36de46dd6b684b8aa43.js:1046
await in perform (async)
start @ application-c20cb2d408231ec629db20f6bfe840b8b84f28b26acee36de46dd6b684b8aa43.js:1258
submitForm @ application-c20cb2d408231ec629db20f6bfe840b8b84f28b26acee36de46dd6b684b8aa43.js:2795
formSubmitted @ application-c20cb2d408231ec629db20f6bfe840b8b84f28b26acee36de46dd6b684b8aa43.js:3504
FormSubmitObserver.submitBubbled @ application-c20cb2d408231ec629db20f6bfe840b8b84f28b26acee36de46dd6b684b8aa43.js:1457

Workaround:

  def create
    create! do |success, failure|
      failure.html { render(:new, status: :unprocessable_entity) }
    end
  end

  def update
    update! do |success, failure|
      failure.html { render(:edit, status: :unprocessable_entity) }
    end
  end

a-nickol avatar Aug 31 '23 05:08 a-nickol