How to return HTTP 204 in a Rails controller

Ruby on-RailsRuby

Ruby on-Rails Problem Overview


This seems to be basic but I'm a Ruby/Rails beginner. I need to simply return HTTP 204 in a controller. Would

respond_to do |format|
  format.html  
end

return a 204?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

head :no_content

Tested with Rails 3.2.x, 4.x. It causes the controller method to respond with the 204 No Content HTTP status code.

An example of using this inside a controller method named foobar:

def foobar
  head :no_content
end

Solution 2 - Ruby on-Rails

Look at the head method:

> Return a response that has no content (merely headers). The options > argument is interpreted to be a hash of header names and values.

Solution 3 - Ruby on-Rails

If you don't want to render anything at all you can do this:

render :nothing => true, :status => 204

or like this:

render :nothing => true, :status => 204 and return

Or you can use the :status => 204 part with any other render command

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNonosView Question on Stackoverflow
Solution 1 - Ruby on-RailsEliot SykesView Answer on Stackoverflow
Solution 2 - Ruby on-RailsMichael KohlView Answer on Stackoverflow
Solution 3 - Ruby on-Railsthorsten müllerView Answer on Stackoverflow