How does Rails calculates the response codes for controller actions?
Given the following controller action:
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to @user, notice: 'User was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'show' }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
(I'm using the same view to show and edit the record)
With this positive test:
test "should update basic user information" do
user = users(:jon)
user.first_name="Jonas"
put :update, :id => user.id, :merchant_user =>user.attributes
assert_response :found
user = Merchant::User.find(user.id)
assert user.first_name == "Jonas", "Should update basic user information"
end
And a negative test is like this:
test "should not update user email for an existing email" do
user = users(:jon)
original_user_email = user.email
existing_user_email = users(:doe)
user.email=existing_user_email.email
put :update, :id => user.id, :merchant_user =>user.attributes
assert_response :success
user = Merchant::User.find(user.id)
assert user.email == original_user_email, "Should not update email for an exising one"
end
Updating the record successfully, causes a 302 response code, which I assume that rails defaults to 302 for the GET resource/:ID. A failure to update the record cause a 200 OK.
How are these response codes being calculated and how can I override them?
Thanks