Return a specific http status code in Rails

80,137

Solution 1

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

Solution 2

You can use head

head 503
# or
head :service_unavailable

Solution 3

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.

Share:
80,137

Related videos on Youtube

Sathish Manohar
Author by

Sathish Manohar

Self Taught. Web Designer, Developer. Building Cool Stuff that I want in this world.

Updated on August 03, 2020

Comments

  • Sathish Manohar
    Sathish Manohar almost 4 years

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

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

  • Sathish Manohar
    Sathish Manohar over 12 years
    To display a custom downpage shall I use, render "custom_unavailable_page", instead of render :nothing => true
  • iwasrobbed
    iwasrobbed over 12 years
    @SathishManohar Exactly. custom_unavailable_page would be the name of the view file that you would render.
  • freemanoid
    freemanoid about 11 years
    I can use status symbols like :service_unavailable All statuses: apidock.com/rails/ActionController/Base/…
  • Sergio Tulentsev
    Sergio Tulentsev about 11 years
    @freemanoid: I personally like integer codes better. I already know them. No need to memorize yet another set of values.
  • Chloe
    Chloe over 10 years
    Where is that documented? What are the other statuses? api.rubyonrails.org/classes/ActionView/Helpers/…
  • iwasrobbed
    iwasrobbed over 10 years
    @Chloe I don't believe it's documented very well, but here's a list apidock.com/rails/ActionController/Base/…
  • juliangonzalez
    juliangonzalez over 7 years
    DEPRECATION WARNING: :nothing option is deprecated and will be removed in Rails 5.1. Use head method to respond with empty response body
  • juliangonzalez
    juliangonzalez over 7 years
    DEPRECATION WARNING: :nothing option is deprecated and will be removed in Rails 5.1. Use head method to respond with empty response body
  • Sergio Tulentsev
    Sergio Tulentsev over 7 years
    @juliangonzalez: yeah, obviously. It's late here, thanks :)
  • heroxav
    heroxav about 7 years
    you saved my life
  • labyrinth
    labyrinth about 7 years
    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: yoast.com/http-503-site-maintenance-seo
  • Joshua Pinter
    Joshua Pinter almost 6 years
    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.
  • Sergio Tulentsev
    Sergio Tulentsev almost 6 years
    @JoshuaPinter: clarified. Sounds better?
  • Joshua Pinter
    Joshua Pinter almost 6 years
    @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.
  • Sergio Tulentsev
    Sergio Tulentsev almost 6 years
    @JoshuaPinter: indeed. what was I thinking?
  • Joshua Pinter
    Joshua Pinter almost 6 years
    @SergioTulentsev Ha! Brilliant! Thanks for being so open to suggestions. That's a really rare trait in programmers. :)
  • mwfearnley
    mwfearnley almost 5 years
    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?