8

Whenever I take in data and do something like delete a post, etc. I generally return head :no_content.

For example:

def destroy
  @post = Post.find_by(external_id: params[:post_id])
  @post.destroy!

  head :no_content
end

Is it good practice to use head :no_content over head :ok? Not too sure when to use either.

David
  • 219
  • 1
  • 2
  • 5

1 Answers1

6

This is the expected behavior in case of a DELETE request. So much so that in rails if you generate a resource or scaffold (using rails generate), this is what is generated.

All 2xx statuses are "success" statuses. So if you are actually not sending any content, it is best to mention it with a no_content

So rest assured that what you are doing is in fact correct and expected.

Vedant Agarwala
  • 775
  • 1
  • 8
  • 16