Home > front end >  Rename an upload file on ruby on rails
Rename an upload file on ruby on rails

Time:11-15

i'm new to ruby ​​on rails, i would like to change the name of an image i upload thank you

      resized_image = MiniMagick::Image.read(picture.image.download)
      resized_image = resized_image.combine_options do |b|
        b.resize '2760>'
        b.quality '80'
      end
      v_filename = picture.image.filename
      v_content_type = picture.image.content_type
      picture.image.purge
      picture.image.attach(io: File.open(resized_image.path), filename: v_filename, content_type: v_content_type)
    end```

CodePudding user response:

As @elmd00 mentioned in the comment picture.image.filename is of class ActiveStorage::Filename so gsub would not work to solve the issue we need to first convert it to string using to_s method then we can use gsub to replace space( ) with underscore(_).

v_filename = v_filename.to_s.gsub(" ", "_")
  • Related