Forum: Ruby on Rails Using after_filter to remove image after render

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
naevity (Guest)
on 2009-02-22 16:41
(Received via mailing list)
Hi Everyone,

I tried doing this and it's not working as I would expect it, but I'm
not sure if it's even possible. I have an app that generates and image
on request and displays it to the user. To cut back on storage, and
some security, I'd like the app to receive the view request, render
the image, display it, then delete the file. I know this isn't the
most efficient way to do things, but it's what I'd like to do.

Right now I have an after filter that removes the file fine, but it
happens before it displays, so I always get a broken image. If I take
that filter out, it works fine.

Am I doing something wrong, or is this just not possible?

My filter looks like this:

def remove
  if logged_in?
    user = User.find(session[:user_id])
    @image = Image.find(params[:id])
    if @image.user_id =
        if File.exist?('PATH TO IMAGE')
            File.delete('PATH TO IMAGE')

and in the controller I have this:

after_filter :remove, :only => [:show]
This topic is locked and can not be replied to.