返回 Rails 中的特定 HTTP状态码

如何在 Rails 中为整个应用程序返回 503服务暂停

另外,如何对特定的控制器执行相同的操作?

85295 次浏览

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

You can use head

head 503
# or
head :service_unavailable

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.