Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving files using Carrierwave without forms

I have 2 models - Album and AlbumImage.

Each album has album images associated with them, and they are uploaded via the AlbumImageUploader class using Carrierwave.

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

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

Any ideas?

like image 654
Jonathan Chiu Avatar asked May 24 '26 12:05

Jonathan Chiu


1 Answers

This is really simple. You have to configure your AlbumCoverUploader the same way as if you would upload it from a form.

Though, to use an image which is 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 grab the image file and use as an input for the AlbumCoverUploader to create its own copy of the image.

like image 184
Lucca Mordente Avatar answered May 26 '26 03:05

Lucca Mordente



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!