How to delete a uploaded file with file_column plugin?

Hi,

sounds somehow stupid, but I can’t get rid of my files that I uploaded
into my application via the file_column plugin. Any suggestions?

Cheers

Thomas

Just destroy the record (which has a file-column) and the file gets
removed.

I’m pretty sure if you set the column that you have defined as your
image to “”, the file will be removed.

jt

I can’t do that, because the ‘file_columned’ record is a user and I
don’t want to destroy the whole user account :wink: Actually I just want to
reset the file column. Maybe I should try it with a own model for
uploaded files - that way I could destroy the related record without any
pain

Thx

Thomas

John T. wrote:

I’m pretty sure if you set the column that you have defined as your
image to “”, the file will be removed.

I tried both “@user.image = ‘’” and “@user.image = nil”. The first one
just does nothing and the second one produces an error. So, no luck this
time :wink:

Cheers

Thomas


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

On 3/27/06, Noel R. Morais [email protected] wrote:

does’nt work!

Hmm… That’s all I had to do. Actually, now that I think about it, I’m
not sure if it did delete the file. I’ll have to do some testing.

jt

does’nt work!

Thomas N. wrote:

I can’t do that, because the ‘file_columned’ record is a user and I
don’t want to destroy the whole user account :wink: Actually I just want to
reset the file column. Maybe I should try it with a own model for
uploaded files - that way I could destroy the related record without any
pain

Thx

Thomas

Modifying the DB schema is indeed the right way to go here for sure,
regardless of how easy/hard it is to work with the image as part of the
user model.

ian parkins wrote:

Modifying the DB schema is indeed the right way to go here for sure,
regardless of how easy/hard it is to work with the image as part of the
user model.

Yeah, I split off things and now have an user and an image table/model.
And it works like a charm :slight_smile: Moreover I declared “user has_many:
images” which is much cooler than before.

Thx

Thomas

I ran into this same issue, and noticed the following:
(assume model User and file_column :file)

after @user.file is called, a variable called @file_state is added to
the @user instance, which contains an instance of PermanentUploadedFile,
the class that file column uses for its operation. If you expose the
@file_state variable, you can then call all the methods in the
PermanentUploadedFile class, including delete_files, which removes the
directory and everything in it for the current file_column.

You can do this by adding:

def state
@file_state
end

to your model and then calling news.state.delete_files

I’ve only just tried this in the console and it worked, but there may be
some bad implications of exposing that variable or other problems. If
some others want to help dig into this, that would be cool.

jared

Here’s a patch which fixes this issue in the library.

— a/vendor/plugins/file_column/lib/file_column.rb
+++ b/vendor/plugins/file_column/lib/file_column.rb
@@ -638,7 +638,11 @@ module FileColumn # :nodoc:
end

   define_method "#{attr}=" do |file|
  •    state = send(state_method).assign(file)
    
  •    if file
    
  •      state = send(state_method).assign(file)
    
  •    else
    
  •      state = send(state_method).delete
    
  •    end
       instance_variable_set state_attr, state
       if state.options[:after_upload] and state.just_uploaded?
         state.options[:after_upload].each do |sym|
    

Once you’ve done this you should be able to set model.delete_file_column
in a form to delete the file.

Jared Moody wrote:

I ran into this same issue, and noticed the following:
(assume model User and file_column :file)

after @user.file is called, a variable called @file_state is added to
the @user instance, which contains an instance of PermanentUploadedFile,
the class that file column uses for its operation. If you expose the
@file_state variable, you can then call all the methods in the
PermanentUploadedFile class, including delete_files, which removes the
directory and everything in it for the current file_column.

You can do this by adding:

def state
@file_state
end

to your model and then calling news.state.delete_files

I’ve only just tried this in the console and it worked, but there may be
some bad implications of exposing that variable or other problems. If
some others want to help dig into this, that would be cool.

jared