【发布时间】:2012-02-12 00:06:38
【问题描述】:
如何在 Rails 中为整个应用程序返回 503 Service Unavailable?
另外,你如何对特定的控制器做同样的事情?
【问题讨论】:
标签: ruby-on-rails http http-status-codes http-status-code-503
如何在 Rails 中为整个应用程序返回 503 Service Unavailable?
另外,你如何对特定的控制器做同样的事情?
【问题讨论】:
标签: ruby-on-rails http http-status-codes http-status-code-503
对于整个应用程序:
# ApplicationController
before_filter :return_unavailable_status
private
def return_unavailable_status
render :nothing => true, :status => :service_unavailable
end
如果您想要自定义错误页面,您可以这样做:
render 'custom_unavailable_page', :status => :service_unavailable
如果您不希望特定控制器使用它:
# SomeController
skip_before_filter :return_unavailable_status
【讨论】:
render "custom_unavailable_page"而不是render :nothing => true来显示自定义的下页
custom_unavailable_page 将是您要渲染的视图文件的名称。
:nothing 选项已弃用,将在 Rails 5.1 中删除。使用head 方法以空响应体响应
你可以使用head
head 503
# or
head :service_unavailable
【讨论】:
:nothing 选项已弃用,将在 Rails 5.1 中删除。使用head 方法以空响应体响应
head,如果您想为后代保留原始答案,只需将其作为“原始答案”或其他内容放在下面。我想我们都同意head 是要走的路,特别是因为render nothing: true 在当前的Rails 版本中已被弃用。
以下对我有用:
format.any { render :json => {:response => 'Unable to authenticate' },:status => 401 }
HTML 响应的:response 以防万一从浏览器访问它。
渲染头 503 似乎不适用于上述语句。
【讨论】: