Skip to content

Commit

Permalink
Respond to PUT/PATCH API request with :ok
Browse files Browse the repository at this point in the history
This commits changes the behavior of the Rails scaffold generator of jbuilder
for successful PUT/PATCH requests on the :json format.

Previously, the scaffold controller would just return :no_content (204).
After this commit, the scaffold controller returns :ok (200) and the entire
updated object as the response body.

The rationale behind this commit is that a PUT, POST or PATCH call may make
modifications to fields of the underlying resource that weren't part of the
provided parameters (for example: created_at or updated_at timestamps).

This commit prevents an API consumer from having to hit the API again for an
updated representation. Rails already provides the created representation after
a POST, so it is coherent to have the same behavior on PUT and PATCH.

This commit is parallel to a pull request to rails/rails to change the
default behavior to respond_to: http://git.io/deL_5g
  • Loading branch information
claudiob committed Sep 4, 2013
1 parent ffcf5d6 commit 9dbce9c
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/generators/rails/templates/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def update
respond_to do |format|
if @<%= orm_instance.update("#{singular_table_name}_params") %>
format.html { redirect_to @<%= singular_table_name %>, notice: <%= "'#{human_name} was successfully updated.'" %> }
format.json { head :no_content }
format.json { render action: 'show', status: :ok, location: <%= "@#{singular_table_name}" %> }
else
format.html { render action: 'edit' }
format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity }
Expand Down
2 changes: 1 addition & 1 deletion test/scaffold_controller_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase

assert_instance_method :update, content do |m|
assert_match /format\.html \{ redirect_to @post, notice: 'Post was successfully updated\.' \}/, m
assert_match /format\.json \{ head :no_content \}/, m
assert_match /format\.json \{ render action: 'show', status: :ok, location: @post \}/, m
assert_match /format\.html \{ render action: 'edit' \}/, m
assert_match /format\.json \{ render json: @post.errors, status: :unprocessable_entity \}/, m
end
Expand Down

0 comments on commit 9dbce9c

Please sign in to comment.