In Ruby on Rails, you must have been used to adding the status: :see_other
after destroying a resource, like this:
def destroy
@testimonial = Testimonial.find(params[:id])
@testimonial.destroy
redirect_to testimonials_path, notice: "Testimonial was successfully deleted", status: :see_other
end
Ever wondered why you have to use the :see_other
303 status instead of a regular 302 Found status?
If you use 302, this means that the HTTP method of the request remains unchanged. Since Rails uses the DELETE method for destroying the resource, that means that the redirected HTTP request would still be a DELETE method.
This means that, the browser will issue a DELETE request to /testimonials
. This is definitely not what we want: we would like the HTTP redirect to issue a GET request.
The 303 See Other status lets us accomplish this. When a browser receives a 303 See Other redirect response, it will issue a GET to the new location.
Note: This doesn't happen on all browsers. I tried to reproduce the problem on Chrome and Safari, and couldn't. The browser did send a GET request after a 302. However, just to be safe, and to follow the best practices, don't forget to redirect with the 303 See Other status after destroying your resources.