How to return HTTP 204 to a Rails controller

This seems to be basic, but I'm new to Ruby / Rails. I just need to return the HTTP 204 to the controller. Will be

respond_to do |format| format.html end 

return 204?

+46
ruby ruby-on-rails
Dec 21 '11 at 16:16
source share
3 answers
 head :no_content 

Tested with Rails 3.2.x, 4.x. This causes the controller method to respond with an HTTP content status code with no content.

An example of using this controller method called foobar :

 def foobar head :no_content end 
+79
Jul 14 '12 at 17:20
source
β€” -

Take a look at the head method:

Returns a response that has no content (headers only). The argument argument is interpreted as a hash of the headers and the values ​​of the headers.

+11
Dec 21 '11 at 16:21
source

If you don't want to do 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 part :status => 204 with any other rendering command

+6
Dec 21
source



All Articles