Skip to content

Commit

Permalink
Return 422 - :unprocessable_entity when form submissions fails (#294)
Browse files Browse the repository at this point in the history
* Adding HTTP Status Code for failed #update action
Co-authored-by: Romanos Fessas <romanos@fessas.com>
  • Loading branch information
deepakmahakale committed Sep 14, 2022
1 parent a46ffb8 commit d360aaa
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
@@ -1,5 +1,6 @@
## Unreleased

* Return `422` (`:unprocessable_entity`) when form submissions fails. Turbo requires an HTTP Status code between 400-499 or 500-599 when a FormSubmission request fails. This pull request makes wicked compatible with Turbo Drive (https://github.com/zombocom/wicked/pull/294)
* Pass the provided step when raising InvalidStepError errors (https://github.com/zombocom/wicked/pull/284)

## 1.4.0
Expand Down
2 changes: 2 additions & 0 deletions lib/wicked/controller/concerns/render_redirect.rb
Expand Up @@ -25,6 +25,8 @@ def process_resource!(resource, options = {})
@skip_to ||= @next_step
else
@skip_to = nil
# Do not override user-provided status for render
options[:status] ||= :unprocessable_entity
end
end

Expand Down
18 changes: 18 additions & 0 deletions test/controllers/status_codes_controller_test.rb
@@ -0,0 +1,18 @@
require 'test_helper'

class StatusCodesControllerTest < WickedControllerTestCase
test 'returns successful status code for show' do
get :show, params: { id: 'good' }
assert_response(:success)
end

test 'returns correct status code for successfuly update' do
put :update, params: { id: 'good' }
assert_response(:redirect)
end

test 'returns correct status code for failed update' do
put :update, params: { id: 'bad' }
assert_response(:unprocessable_entity)
end
end
39 changes: 39 additions & 0 deletions test/dummy/app/controllers/status_codes_controller.rb
@@ -0,0 +1,39 @@
class StatusCodesController < ApplicationController
include Wicked::Wizard

class GoodThing
def save
true
end
end

class BadThing
def save
false
end
end

steps :good, :bad

def index; end

def show
render_wizard
end

def update
case step
when :good
@thing = GoodThing.new
when :bad
@thing = BadThing.new
end
render_wizard(@thing, notice: "Thing was updated from step #{step}.")
end

private

def finish_wizard_path
updates_path
end
end
1 change: 1 addition & 0 deletions test/dummy/app/views/status_codes/bad.html.erb
@@ -0,0 +1 @@
bad step
1 change: 1 addition & 0 deletions test/dummy/app/views/status_codes/good.html.erb
@@ -0,0 +1 @@
good step
1 change: 1 addition & 0 deletions test/dummy/config/routes.rb
Expand Up @@ -8,6 +8,7 @@
resources :string_steps
resources :redirect_to_next
resources :redirect_to_finish_flash
resources :status_codes
resources :updates
resources :update_params

Expand Down

0 comments on commit d360aaa

Please sign in to comment.