Return a specific http status code in Rails

How to return 503 Service Unavailable to Rails for the whole application?

Also, how will you do the same for certain controllers?

+52
Jan 17 '12 at 5:59
source share
3 answers

For the whole application:

# ApplicationController before_filter :return_unavailable_status private def return_unavailable_status render :nothing => true, :status => :service_unavailable end 

If you want to create a custom error page, you can do the following:

 render 'custom_unavailable_page', :status => :service_unavailable 

If you do not want to use it for certain controllers:

 # SomeController skip_before_filter :return_unavailable_status 
+59
Jan 17 '12 at 6:13
source

You can use :status

 render :status => 503 

You can do it globally by putting it in the ApplicationController

 before_filter :render_unavailable def render_unavailable render :nothing => true, :status => 503 end 

Update

Rails 5+

 head 503 # or head :service_unavailable 

Do not put render head: 503 only the head. Otherwise, you will get a double rendering error.

+68
Jan 17 '12 at 6:13
source

The following works for me:

 format.any { render :json => {:response => 'Unable to authenticate' },:status => 401 } 

:response to respond to HTML in case it is available from a browser.

The render head 503 does not seem to work with the above statement.

-one
Apr 20 '17 at 20:14
source



All Articles