Return a specific http status code in Rails

后端 未结 3 1586
无人及你
无人及你 2021-01-31 01:08

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

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

3条回答
  •  清酒与你
    2021-01-31 01:52

    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
    

提交回复
热议问题