views:

45

answers:

2

The performed? method returns true in a controller if the application has redirected or rendered.

How can I determine whether the application rendered (rather than redirected)?

Judging from the performed? docs, I can look at @performed_render, but this seems hacky.

Edit: Why do I want to do this?

I'm trying to cache the output of a controller action only in the case that it results in a render (and not a redirect). Here's the code I'm using now: (based on render-caching)

  def render_with_cache(options = {})
    key = ([request.request_uri, request.format] | (options.delete(:key_additions) || [])).join("::")
    body = Rails.cache.read(key)

    if body
      render :text => body
    else
      yield if block_given?
      render unless performed?
      if @performed_render
        Rails.cache.write(key, response.body, options) # :expires_in
      end
    end
  end
+1  A: 

Look at @performed_render. :) You don't explain why you need to detect this, so I am unable to suggest alternative solutions.

August Lilleaas
A: 

In an after filter, check the codes for the response status .

MyController < ActionController
  after_filter :check_response

  # define your actions that render or redirect

  protected

  def check_response
    # put real app logic here
    puts :rendered! if response.status == "200 OK" 
    puts :redirected! if response.status == "302 Found" 

  end
end
glongman
`@perfomred_render` is superior. This will fail if you do a 301, for example.
August Lilleaas
not so. works fine for me. the response status is "301 Moved Permanently"
glongman