R
R
Ruslan Galiev2014-07-17 10:01:12
Ruby on Rails
Ruslan Galiev, 2014-07-17 10:01:12

Rails + Carrierwave how to move to another directory and rename files?

Hello
I ran into the following problem in a working project. It is necessary to change the directory structure for images loaded via carrierwave. Rested against the ceiling of the nix file system in 32000 directories.
Now the files are uploaded to the directory:

def store_dir
    "uploads/#{model.class.to_s.underscore}/#{model.id}/"
end
def filename
    "photo.jpg" if original_filename
  end

And it needs to become:
def store_dir
    "uploads/#{model.class.to_s.underscore}/"
end

Plus rename the files to:
def filename
    "#{secure_token}.jpg" if original_filename
  end
  
  protected
  def secure_token
    var = :"@#{mounted_as}_secure_token"
    model.instance_variable_get(var) or model.instance_variable_set(var, SecureRandom.uuid)
  end

But if I just change the directory, then the links to the old files become invalid. Found a solution on stackoverflow . I did everything one to one, I execute the script:
RoutePointPhoto.all.each do |image|
  image.photo.cache! 
  image.photo.store!
end

I get an error:
ArgumentError: wrong number of arguments (1 for 0)
        from /team/mss/app/uploaders/route_point_photo_uploader.rb:30:in `swap_out_store_dir'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ruslan Galiev, 2014-07-17
@HerMajor

In general, I solved the problem in two stages:
1) Rename the files as described here
2) Move the files with the command
3) Change the directory in the uploader file

def store_dir
    "uploads/#{model.class.to_s.underscore}/"
end

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question