开发者

Detect in Rails after_filter whether we're rendering or redirecting

开发者 https://www.devze.com 2023-03-09 14:32 出处:网络
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

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
0

精彩评论

暂无评论...
验证码 换一张
取 消