I'm writing 开发者_运维百科an after_filter in Rails 3, and I'd like to detect whether or not the controller (or any other filter) has issued a redirect. Is there any way to do this?
You could look at the status
code. 200 is a render, 302 is a redirect.
after_filter :what_happened
protected
def what_happened
was_redirect = self.status == 302
was_render = self.status == 200
end
For anyone who is still stuck on rails 3, and wants to use a class based filter to share the logic between controllers with composition, check out the following example:
class MyController < ApplicationController
after_filter ControllerMetrics
end
class ControllerMetrics
def self.filter(controller)
status_xxx = "#{controller.response.status / 100}XX"
Statsd.increment("response.#{status_xxx}")
end
end
精彩评论