Forum: Ruby on Rails Getting HTTP response code from a controller

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Mark (Guest)
on 2007-03-21 03:29
Hi there, how would I get the HTTP response code (200 OK, 500, etc...)
from a controller after a request have been 'served'?


Thanks
Mark (Guest)
on 2007-03-21 05:01
I was able to get the response status after_filter by doing
response.headers["Status"], but it *only* displays the 200 and 302
codes. Any ideas how I get get the others (404, 500, etc...) as well?

Thanks


Mark wrote:
> Hi there, how would I get the HTTP response code (200 OK, 500, etc...)
> from a controller after a request have been 'served'?
>
>
> Thanks
Eden L. (Guest)
on 2007-03-21 06:15
(Received via mailing list)
You'll have to hook it somewhere else (eg, in Mongrel or a monkey/
mixin patch to ActionController::Base).  Your controller won't be
consulted in the case of a 404 or 500 error.  Those are usually
generated from exceptions.

Is there a reason you need to know about these status codes in your
controller?
Mark (Guest)
on 2007-03-21 06:36
Eden Li,

I see what you mean. The reason why I need those status codes is for a
personal project that I'm doing and I need to store those codes in the
DB as-they-happen.

Could you give me a starting point for the monkey/mixin patch?

Thanks


Eden Li wrote:
> You'll have to hook it somewhere else (eg, in Mongrel or a monkey/
> mixin patch to ActionController::Base).  Your controller won't be
> consulted in the case of a 404 or 500 error.  Those are usually
> generated from exceptions.
>
> Is there a reason you need to know about these status codes in your
> controller?
(Guest)
on 2007-03-21 06:43
(Received via mailing list)
Cool, I could have a use for that as well. Let me know if you guys
figure it out. In the meantime I'll try to do it too.

--
Thiago J.
acts_as_solr => http://acts-as-solr.rubyforge.org
Sitealizer Web Stats => http://sitealizer.rubyforge.org
Eden L. (Guest)
on 2007-03-21 09:34
(Received via mailing list)
Here's a first stab...

# lib/final_response.rb
module FinalResponse
  def self.included(base)
    base.class_eval do
      alias_method :process_without_final_response, :process
      alias_method :process, :process_with_final_response
    end
  end

  def process_with_final_response(*args)
    process_without_final_response(*args)
  ensure
    final_response if respond_to?(:final_response)
  end
end

# app/controllers/my_controller.rb
class MyController < ActionController::Base
  include FinalResponse
  def final_response
    logger.info("status = #{headers['Status']}")
  end

  def action
    raise
  end
end

Your final_response method won't have access to the session, but
you'll still be OK to access models.

There's probably a better way to do this, but this should work...
This topic is locked and can not be replied to.