86

How do you return 503 Service Unavailable in Rails for the entire application?

Also, how do you do the same for specific controllers?

iwasrobbed
  • 46,496
  • 21
  • 150
  • 195
Sathish Manohar
  • 5,859
  • 10
  • 37
  • 47

3 Answers3

112

You can use head

head 503
# or
head :service_unavailable
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
  • 11
    I can use status symbols like :service_unavailable All statuses: http://apidock.com/rails/ActionController/Base/render#254-List-of-status-codes-and-their-symbols – freemanoid May 22 '13 at 14:29
  • 1
    @freemanoid: I personally like integer codes better. I already know them. No need to memorize yet another set of values. – Sergio Tulentsev May 22 '13 at 20:05
  • 3
    DEPRECATION WARNING: `:nothing` option is deprecated and will be removed in Rails 5.1. Use `head` method to respond with empty response body – juliangonzalez Feb 20 '17 at 15:19
  • FYI, `head` has been available since Rails 1.2.0. Saying "Rails 5+" implies it's only available in Rails 5 and beyond, which is incorrect. – Joshua Pinter Aug 01 '18 at 22:34
  • 1
    @SergioTulentsev I would just update the answer to use `head` and if you want to keep the original answer for posterity, just put it below as "Original Answer" or something. I think we can all agree that `head` is the way to go, especially since `render nothing: true` is deprecated on current Rails versions. – Joshua Pinter Aug 02 '18 at 16:40
  • It would help if you said _where to put this_. – JakeRobb Aug 31 '22 at 21:20
89

For the entire application:

# ApplicationController
before_filter :return_unavailable_status

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

If you wanted a custom error page, you could do:

render 'custom_unavailable_page', :status => :service_unavailable    

If you don't want it for specific controllers:

# SomeController
skip_before_filter :return_unavailable_status
iwasrobbed
  • 46,496
  • 21
  • 150
  • 195
  • To display a custom downpage shall I use, `render "custom_unavailable_page"`, instead of `render :nothing => true` – Sathish Manohar Jan 17 '12 at 06:39
  • 1
    @SathishManohar Exactly. `custom_unavailable_page` would be the name of the view file that you would render. – iwasrobbed Jan 17 '12 at 06:52
  • 1
    Where is that documented? What are the other statuses? http://api.rubyonrails.org/classes/ActionView/Helpers/RenderingHelper.html#method-i-render – Chloe Jan 22 '14 at 03:54
  • 3
    @Chloe I don't believe it's documented very well, but here's a list http://apidock.com/rails/ActionController/Base/render#254-List-of-status-codes-and-their-symbols – iwasrobbed Jan 22 '14 at 13:18
  • 1
    DEPRECATION WARNING: `:nothing` option is deprecated and will be removed in Rails 5.1. Use `head` method to respond with empty response body – juliangonzalez Feb 20 '17 at 15:19
2

The following works for me:

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

The :response for the HTML response just in case it's accessed from the browser.

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

  • 1
    This would be bad for SEO. For example, Google would see this as a broken site, not just a site temporarily down but expected to be back up. See this: https://yoast.com/http-503-site-maintenance-seo/ – labyrinth Jun 12 '17 at 18:36
  • Might be worth changing this to "Service unavailable" / 503, so it matches with the purpose of the question. I assume it's the "render head" syntax that isn't working for you, rather than the error code? – mwfearnley Aug 20 '19 at 11:48