From b2dd0a60e8d232292029ea0f10d69702f8c88458 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Sun, 24 Jan 2021 20:42:25 -0300 Subject: [PATCH] Update method docs to reflect the new 422 response on unsuccessful cases --- README.md | 3 ++- lib/action_controller/respond_with.rb | 9 +++++---- lib/action_controller/responder.rb | 4 ++-- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 7cd3cbf..f8b28f9 100644 --- a/README.md +++ b/README.md @@ -212,7 +212,8 @@ assertions on this behavior for your controllers. def create @widget = Widget.new(widget_params) @widget.errors.add(:base, :invalid) - # `respond_with` will render the `new` template again. + # `respond_with` will render the `new` template again, + # and set the status to `422 Unprocessable Entity`. respond_with @widget end ``` diff --git a/lib/action_controller/respond_with.rb b/lib/action_controller/respond_with.rb index 7d3d01e..1da4a05 100644 --- a/lib/action_controller/respond_with.rb +++ b/lib/action_controller/respond_with.rb @@ -95,7 +95,8 @@ def clear_respond_to # i.e. its +show+ action. # 2. If there are validation errors, the response # renders a default action, which is :new for a - # +post+ request or :edit for +patch+ or +put+. + # +post+ request or :edit for +patch+ or +put+, + # and the status is set to 422 Unprocessable Entity. # Thus an example like this - # # respond_to :html, :xml @@ -116,8 +117,8 @@ def clear_respond_to # format.html { redirect_to(@user) } # format.xml { render xml: @user } # else - # format.html { render action: "new" } - # format.xml { render xml: @user } + # format.html { render action: "new", status: :unprocessable_entity } + # format.xml { render xml: @user, status: :unprocessable_entity } # end # end # end @@ -194,7 +195,7 @@ def clear_respond_to # need to render a template which is outside of controller's path or you # want to override the default http :status code, e.g. # - # respond_with(resource, render: { template: 'path/to/template', status: 422 }) + # respond_with(resource, render: { template: 'path/to/template', status: 418 }) def respond_with(*resources, &block) if self.class.mimes_for_respond_to.empty? raise "In order to use respond_with, first you need to declare the " \ diff --git a/lib/action_controller/responder.rb b/lib/action_controller/responder.rb index 6b5732e..5751458 100644 --- a/lib/action_controller/responder.rb +++ b/lib/action_controller/responder.rb @@ -49,7 +49,7 @@ module ActionController #:nodoc: # format.html { redirect_to(@user) } # format.xml { render xml: @user, status: :created, location: @user } # else - # format.html { render action: "new" } + # format.html { render action: "new", status: :unprocessable_entity } # format.xml { render xml: @user.errors, status: :unprocessable_entity } # end # end @@ -113,7 +113,7 @@ module ActionController #:nodoc: # if @task.save # flash[:notice] = 'Task was successfully created.' # else - # format.html { render "some_special_template" } + # format.html { render "some_special_template", status: :unprocessable_entity } # end # end # end