# Using after\_filter to remove image after render

**URL:** <https://www.ruby-forum.com/t/using-after-filter-to-remove-image-after-render/161029>\
**Category:** Rails\
**Created:** [February 22, 2009, 3:41pm UTC](https://www.ruby-forum.com/t/using-after-filter-to-remove-image-after-render/161029 "2009-02-22T15:41:00Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![naevity](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e9a140/32.png) [@naevity](https://www.ruby-forum.com/u/naevity)\
**Post date:** [February 22, 2009, 3:41pm UTC](https://www.ruby-forum.com/t/using-after-filter-to-remove-image-after-render/161029/1 "2009-02-22T15:41:00Z")

</div>

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 = user.id  
if File.exist?(‘PATH TO IMAGE’)  
File.delete(‘PATH TO IMAGE’)  
end  
end  
end  
end

and in the controller I have this:

after\_filter :remove, :only =\> [:show]
