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 process开发者_StackOverflow社区ed 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?
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.
精彩评论