Carrierwave: set image path and skip download

I would like to install some images without downloading. (They already exist or another task saves them ...)

If I try (in the rails console):

user = User.last user.picture = '/some/picture.jpg' user.save user.picture # nil 

The only way to do this is to set remote_picture_url and then remove the download (which is silly)

Is there any method in the carrier that allows you to change only the file name?

+4
source share
1 answer
 class User < ActiveRecord::Base attr_accessible :picture # Don't want to write to the database, but want to be able to check attr_writer :skip # set a default value def skip @skip ||= false end mount_uploader :image, PictureUploader # Make sure that the skip callback comes after the mount_uploader skip_callback :save, :before, :store_picture!, if: :skip_saving? # Other callbacks which might be triggered depending on the usecase #skip_callback :save, :before, :write_picture_identifier, id: :skip_saving? def skip_saving? skip end end class PictureUploader < Carrierwave::Uploader::Base # You could also implement filename= def set_filename(name) @filename = name end end 

Assuming you have the setting above, on the console:

 user = User.last user.picture.set_filename('/some/picture.jpg') user.skip = true # Save will now skip the callback store_picture! user.save user.picture # /some/picture.jpg 

It should be noted that if you are on the console and are updating an existing entry that has an attached file (for example, user.picture.file), it will show the old URL / location. If you leave the console (if you are not in sandbox mode) and go back and request the same object, it will have an updated URL / location.

+5
source

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


All Articles