Forum: Ruby on Rails Clearing a file_column-type field

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.
32b703cdb0ea03c3939b97a93205414f?d=identicon&s=25 John T. (johnt519)
on 2006-01-23 03:27
So I am now able to set/upload an image to my app using file_column,
with a little bit of a change to the code.

I can change the image using an Edit page, but I'm trying to figure out
what to do if the user wants to remove the image. I tried adding a
Remove Image link in my list, but I'm not sure what to do in the
remove_image method in my controller. I tried setting the 'image' value
to ''; I tried using update_attribute to set to '' and neither did
anything. The SQL update command that was logged just showed it
replacing the records values with the same data - ie. not changing
anything.

I tried doing an update_attribute to "", but I got an error from
file_column:

(Do not know how to handle a string with value 'nothing' that was passed
to a file_column. Check if the form's encoding has been set to
'multipart/form-data'.)

Yeah, newbie... ;)

(And I haven't even added a second relationship table to my app
yet..figured this would be the easy part.)

Thanks.

jt
727d5785259d057745de13a9f3a713f4?d=identicon&s=25 David Rose (Guest)
on 2006-01-23 06:22
(Received via mailing list)
This works for me:

ModelName.find(your_parameters_here).update_attribute
:your_image_column_name, nil

doppler
54077fb03c95861af85880218cf7aef2?d=identicon&s=25 Gokhan Arli (sylow)
on 2006-01-23 08:26
David Rose wrote:
> This works for me:
>
> ModelName.find(your_parameters_here).update_attribute
> :your_image_column_name, nil
>
> doppler

@user = User.find(1)
@user.your_file_column = nil
@user.save


Gokhan Arli
www.sylow.net
32b703cdb0ea03c3939b97a93205414f?d=identicon&s=25 John T. (johnt519)
on 2006-01-23 08:48
Thanks! Those both worked.

I had something like that, but was getting an error - which actually
turned out to be in my "show" method - it was trying to display the
image tag, but the field was nil. Now it checks and works.

jt
This topic is locked and can not be replied to.