Saving Files Using Carrierwave Without Forms

I have 2 models - Album and AlbumImage.

Each album has albums associated with them, and they are loaded through the AlbumImageUploader class using Carrierwave.

Now I want to select an album cover for each album using existing linked album images. I need to process (crop and resize) the selected image before using it as an album cover. I have cropping and resizing functions, and I created the AlbumCoverUploader class to save this processed version of the album image.

The problem is that this time I do not use the form to upload a new image file and instead use the existing album image in the file system, and I'm not sure how to transfer this image from the AlbumImageUploader class to my AlbumCoverUploader Class.

Any ideas?

+3
source share
1 answer

It is really easy. You must configure your AlbumCoverUploader in the same way as if you downloaded it from a form.

However, to use the image associated with an existing record, you must do the following:

album = Album.find(id)                  # your existing album
album_image = album.album_images.first  # the image you want as cover
album.cover = File.open(album_image.image.current_path)
album.save

This will capture the image file and will be used as input for AlbumCoverUploader to create its own copy of the image.

+1
source

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


All Articles