Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return HTTP 204 in a Rails controller

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?

like image 330
Nonos Avatar asked Dec 21 '11 16:12

Nonos


People also ask

Can you send body 204?

HTTP Status 204 (No Content) indicates that the server has successfully fulfilled the request and that there is no content to send in the response payload body.

What does render JSON do in Rails?

render :json essentially calls to_json and returns the result to the browser with the correct headers. This is useful for AJAX calls in JavaScript where you want to return JavaScript objects to use. Additionally, you can use the callback option to specify the name of the callback you would like to call via JSONP.


2 Answers

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 
like image 99
Eliot Sykes Avatar answered Sep 21 '22 03:09

Eliot Sykes


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.

like image 40
Michael Kohl Avatar answered Sep 22 '22 03:09

Michael Kohl