Return a specific http status code in Rails

孤街醉人 提交于 2019-12-03 04:05:57

问题


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

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


回答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



回答2:


You can use head

head 503
# or
head :service_unavailable



回答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.



来源:https://stackoverflow.com/questions/8890351/return-a-specific-http-status-code-in-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!