Forum: Ruby on Rails How to delete a uploaded file with file_column plugin?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Thomas N. (Guest)
on 2006-03-26 20:50
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
Roberto S. (Guest)
on 2006-03-26 20:55
(Received via mailing list)
Just destroy the record (which has a file-column) and the file gets
removed.
Thomas N. (Guest)
on 2006-03-26 21:10
I can't do that, because the 'file_columned' record is a user and I
don't want to destroy the whole user account ;-) 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. (Guest)
on 2006-03-27 03:43
(Received via mailing list)
I'm pretty sure if you set the column that you have defined as your
image to "", the file will be removed.

jt
Thomas N. (Guest)
on 2006-03-27 12:36
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 ;-)

Cheers

Thomas
Mats L. (Guest)
on 2006-03-27 12:43
(Received via mailing list)
_______________________________________________
Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails
Noel R. Morais (Guest)
on 2006-03-27 16:43
(Received via mailing list)
does'nt work!
John T. (Guest)
on 2006-03-28 11:20
(Received via mailing list)
On 3/27/06, Noel R. Morais <removed_email_address@domain.invalid> 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
Ian P. (Guest)
on 2006-03-28 12:06
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 ;-) 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.
Thomas N. (Guest)
on 2006-03-28 23:58
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 :-) Moreover I declared "user has_many:
images" which is much cooler than before.

Thx

Thomas
Jared M. (Guest)
on 2006-10-31 21:14
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
Daniel H. (Guest)
on 2009-12-14 07:42
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
This topic is locked and can not be replied to.