Return a specific http status code in Rails Return a specific http status code in Rails ruby-on-rails ruby-on-rails

Return a specific http status code in Rails


You can use head

head 503# orhead :service_unavailable


For the entire application:

# ApplicationControllerbefore_filter :return_unavailable_statusprivate  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:

# SomeControllerskip_before_filter :return_unavailable_status


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.