How to display thumbnail in Carrierwave cache

I use Carrierwave so that users can attach images to their posts. In the message list, I show the thumbnails (in haml) as follows:

%td= image_tag post.image.url :thumb 

If the verification fails when creating a new message, I will show the cache as follows:

 = image_tag "/#{ImageUploader::cache_dir}/#{post.image_cache}" 

I cannot figure out how to display the cache file. Checking the file system confirms that it lives in the same directory as the cached image. I tried

 = image_tag post.image_cache :thumb 

but errors with wrong number of arguments (1 for 0)

+4
source share
2 answers

Ok, finally figured it out.

  • Carrierwave loads caching even in the event of a validation error. After all, what is the cache for? This eliminates the need for the file to be reloaded.

  • Pearl users (me, the programmer) need not worry about how to access the cache. Just specify the cache field in the form and attr_accessible and get access to the image (or any other file), as usual. Carrierwave does the rest transparently. So in my case

     %td= image_tag post.image.url :thumb 

will display the image correctly, either from the store catalog or from the cache.

Carrierwave on github

+5
source

No, because AFAIK Carrierwave does not cache the version if there is a validation error

If I'm right, Carrierwave caches the version and processes it in the after callback and

validation validation is performed in the before callback.

you can something like this

  before_callback :check_for_validation // do some logic after_callback :cache_the_version and process it 

Any error in the previous callback causes Carrierwave to bypass the last callback chain

and therefore, you do not have a version cached for stumbling verification errors. (Which I consider to be expected behavior, as I think about it in order to process and create the version if they are not valid )

Error

wrong number of arguments (1 for 0)

This is the expected behavior, too, because #{column}_cache in your case, the image_cache method image_cache not accept any arguments

I don’t think there is any way to display the version cache image ( thumb in your case), given that the version is never cached after receiving a validation error

Correct me if I am wrong :)

Hope for this help

+1
source

Source: https://habr.com/ru/post/1495098/


All Articles