Skip to content

Response Codes

nesquena edited this page Oct 22, 2012 · 4 revisions

Rails

https://github.com/nesquena/rabl/issues/88

class MyResponder < ActionController::Responder
  def to_format
    controller.response.status = :created if post?
    super
  end
end

and then in the controller:

class ApplicationController < ActionController::Base
  respond_to :json, :xml, :html

  self.responder = MyResponder
end

Setting controller.response.status is like a default value. It will be overridden by render/respond_with options, and by the super to_format for cases like has_errors?. You can add other default codes here as well, e.g.:

class KeydropResponder < ActionController::Responder
  def to_format
    if get? && !resource
      controller.response.status = :not_found
    elsif post?
      controller.response.status = :created
    end
    super
  end
end
Something went wrong with that request. Please try again.