Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload removed when update without file field #2362

Closed
Sh1n1x opened this issue Dec 28, 2018 · 8 comments
Closed

Upload removed when update without file field #2362

Sh1n1x opened this issue Dec 28, 2018 · 8 comments

Comments

@Sh1n1x
Copy link

Sh1n1x commented Dec 28, 2018

Hi,

I have a uploader for "img" field in activerecord. If this field isn't used on my form carrierwave remove image into the database.

Here's an example :

<% if @piece.img? %>
  <%= image_tag img(@piece.img.thumb.url) %>
  <%= f.check_box :remove_image, label:"Supprimer l'image"%>
<% else %>
  <%= f.input :img, label: "Image de la pièce" %>
<% end %>

If this img input isn't present, carrierwave clear this field into database and if i remove from controller the private params of this field. I cannot upload but if he's already an image uploaded, carrierwave remove the image without permission...

I use rails (5.2.2) with carrierwave (1.3.0).

It's a bug with carrierwave ?

@cireficc
Copy link

cireficc commented Mar 7, 2019

Having the same issue, extremely frustrating. Carrierwave will delete the existing upload unless you put it into the file selection box.

Basically, editing a model record with a Carrierwave upload results in the upload being deleted unless you re-upload the file during the edit. Don't know how this bug came about, and I can't find a workaround (been trying for hours).

@psavard
Copy link

psavard commented Apr 8, 2019

Hello,

I'm having the same issue and I would like to know if there is any work around or fix I can apply to solve this problem?

I'm on version 1.2.0

Thanks

@psavard
Copy link

psavard commented Apr 8, 2019

Finally, by simply skipping the field if it is "empty", the file is not removed anymore.

This workaround is ok with me!

@cireficc
Copy link

Finally, by simply skipping the field if it is "empty", the file is not removed anymore.

This workaround is ok with me!

What do you mean by "skipping the field if it is empty"? Do you mean a model validation, controller, what? How are you determining if it is "empty"?

@psavard
Copy link

psavard commented Apr 15, 2019

Well, in fact if you do select the file in the html form should contain an image object. If you do NOT select a file, the field for the file will only contain a string with the filename of the actual file. So, to make sure my file is not deleted when I save my changes, I do something like this on the update action of my controller

if(params[:my_object][:my_field].kind_of?(String))
    params[:my_object] = params[:my_object].reject {|k, v| k == 'my_field' }
end

Doing this, this field is skipped when the save action is done.

Hope this helps!

@mshibuya
Copy link
Member

This is not supposed to happen, what do you have in your uploader and model?

@fsmanuel
Copy link

@mshibuya In my case it was connected to the cache field.

If I have a hidden field for the image_cache and it is empty the hidden field will send a parameter "image_cache"=>"" that overwrites the not present image parameter and delete the uploaded image.

In my case I fixed it by only sending the image_cache parameter if it is set:

  - if f.object.image_cache
    = f.input :image_cache, as: :hidden

I hope it helps to find the bug.

@mshibuya
Copy link
Member

Sorry for getting back late, this is dup of #2412 which was already fixed.

@mshibuya mshibuya closed this as not planned Won't fix, can't repro, duplicate, stale Dec 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants