Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

carrierwave cache not resubmitting

When I submit an image via Carrierwave, and am missing one field (i.e. the title), then the cached image will appear...

      <% if @post.avatar? %>
          <%= image_tag @post.avatar_url, :style => "width:300px"  %> 
          <%= f.hidden_field :avatar_cache %>
          <%= @post.avatar_url %>
      <% end %>

However, the :avatar_cache field is empty. When I resubmit the form, none of the image properties move forward, so I have to reselect the image.

The issue is similar to this. CarrierWave not saving upload after form redisplay but there was no answer.

What is happening? Thank you.

like image 646
user749798 Avatar asked May 08 '13 01:05

user749798


2 Answers

For me, the issue was that I had

accepts_nested_attributes_for :avatars, allow_destroy: true, reject_if: lambda { |avatar| avatar[:file].blank? }

So I was rejecting the file because the file wasn't there

It is important to note that the file itself does not persist, but only the file cache. That is why the carrierwave docs suggest:

It might be a good idea to show the user that a file has been uploaded, in the case of images, a small thumbnail would be a good indicator:

like image 86
stevenspiel Avatar answered Oct 10 '22 04:10

stevenspiel


Check if you have an attr_accessor declaration for avatar_cache in your model. I added it by accident instead of attr_accessible (as mentioned in their docs) and it overrode the methods generated by CarrierWave.

like image 3
Ivan Poliakov Avatar answered Oct 10 '22 04:10

Ivan Poliakov