# How do I delete a FlexImage entry

**URL:** <https://www.ruby-forum.com/t/how-do-i-delete-a-fleximage-entry/112962>\
**Category:** Rails\
**Created:** [August 25, 2007, 12:55am UTC](https://www.ruby-forum.com/t/how-do-i-delete-a-fleximage-entry/112962 "2007-08-25T00:55:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [August 25, 2007, 12:55am UTC](https://www.ruby-forum.com/t/how-do-i-delete-a-fleximage-entry/112962/1 "2007-08-25T00:55:00Z")

</div>

I am using fleximage to handle images. Its great. I can upload, edit  
and view the image just fine. But, now I want to allow users to delete  
their image. Can someone please explain how.

In the UI when a user is editing the model with the image, currently I  
display the image and give users the option to upload a new file. I am  
changing it so that a user selects to either keep the image, upload a  
new image or clear their image. In the controller I check if the clear  
image param was passed in and then delete the image from the table.  
I have tried:  
@mod.data = nil  
this returns the error:  
Exception in edit\_module: You have a nil object when you didn’t expect  
it!  
You might have expected an instance of Array.  
The error occured while evaluating nil.size

It looks like it is trying to resize the image which is now nil.

Also tried:

@mod.data = ’ ’  
This didn’t do anything

I am all ears on how to delete the image. Thanks.

## Model

class LibResource \< FlexImage::Model  
self.require\_image\_data = false  
pre\_process\_image :size =\> ‘250x250’

## Controller

if request.post?  
@mod.attributes = params[:mod] #updates the model as usual

…  
if params[:image\_options]  
option = params[:image\_options]  
if option == ‘Remove my image’  
@mod.data = nil  
@mod.save!  
end  
end

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [August 26, 2007, 12:44am UTC](https://www.ruby-forum.com/t/how-do-i-delete-a-fleximage-entry/112962/2 "2007-08-26T00:44:36Z")

</div>

Bump. Anyone ever had to delete a fleximage form the table. Tell me  
how you did it.

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [September 3, 2007, 5:25am UTC](https://www.ruby-forum.com/t/how-do-i-delete-a-fleximage-entry/112962/3 "2007-09-03T05:25:23Z")

</div>

Hey Kim,

I am by no means a rails expert, so there’s probably a dozen things  
wrong with my code, but it at least works. Here’s how I delete my  
fleximage pictures from both the database and the filesystem:

In my view, I have a checkbox for each image, named “photo\_delete” and  
having the values of the table id for that image. So  
params[:photo\_delete] is a hash of all the id’s of the images the user  
wants to delete.

Then, in my controller:

```
@delete = Photo.find(:all, :conditions => { :id =>

```

params[:photo\_delete] }) if params[:photo\_delete]  
if @delete  
@delete.each do |delete|  
#----------------------Clean this up to automatically get the photo  
extension type-----------------------------------  
File.delete("#{RAILS\_ROOT}/public/images/#{delete.id}.jpg")  
@message = "Photos have been deleted. " if delete.destroy  
end

I hope this helps with what you are trying to do.

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [September 3, 2007, 5:27am UTC](https://www.ruby-forum.com/t/how-do-i-delete-a-fleximage-entry/112962/4 "2007-09-03T05:27:23Z")

</div>

And, uh, try to ignore the line-breaks in the code that were  
automatically added to my post.
