Search code examples
ruby-on-railsstatushead

What's the difference between rendering a status code and using head?


In a tutorial, I've seen both used. Besides the fact that it seems better to use head for a delete request, what are the real reasons between this and render status: 200?

Other examples:

  1. render json: user, status: 200, location: [:api, user]

  2. head 204


Solution

  • head returns no body in the response. It's an http response with specific code that's it. If you expect for example a json response (as in the above example, you serialise the user object to a json), this will not work for you.

    If all you need is good/bad, head is perfect and is very light weight. if for example you want to actually return a created/deleted object (just as an example) you should use render.