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.
Related videos on Youtube
Author by
Sathish Manohar
Self Taught. Web Designer, Developer. Building Cool Stuff that I want in this world.
Updated on August 03, 2020Comments
-
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 over 12 yearsTo display a custom downpage shall I use,
render "custom_unavailable_page"
, instead ofrender :nothing => true
-
iwasrobbed over 12 years@SathishManohar Exactly.
custom_unavailable_page
would be the name of the view file that you would render. -
freemanoid about 11 yearsI can use status symbols like :service_unavailable All statuses: apidock.com/rails/ActionController/Base/…
-
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 over 10 yearsWhere is that documented? What are the other statuses? api.rubyonrails.org/classes/ActionView/Helpers/…
-
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 over 7 yearsDEPRECATION WARNING:
:nothing
option is deprecated and will be removed in Rails 5.1. Usehead
method to respond with empty response body -
juliangonzalez over 7 yearsDEPRECATION WARNING:
:nothing
option is deprecated and will be removed in Rails 5.1. Usehead
method to respond with empty response body -
Sergio Tulentsev over 7 years@juliangonzalez: yeah, obviously. It's late here, thanks :)
-
heroxav about 7 yearsyou saved my life
-
labyrinth about 7 yearsThis 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 almost 6 yearsFYI,
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 almost 6 years@JoshuaPinter: clarified. Sounds better?
-
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 thathead
is the way to go, especially sincerender nothing: true
is deprecated on current Rails versions. -
Sergio Tulentsev almost 6 years@JoshuaPinter: indeed. what was I thinking?
-
Joshua Pinter almost 6 years@SergioTulentsev Ha! Brilliant! Thanks for being so open to suggestions. That's a really rare trait in programmers. :)
-
mwfearnley almost 5 yearsMight 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?