Hello!

How can I remove img files from the '/public/posts/' directory? There are several files in this folder:

 -public --posts ---1.jpg ---2.jpg ---3jpg 

Model file:

Post.rb

 before_destroy :remove_file def remove_file getAllImages.each do |file| File.delete(Rails.root.join('public', file)) end end 

Error:

 Errno::ENOENT in PostsController#destroy No such file or directory @ unlink_internal - /posts/1.jpg 

    1 answer 1

    Your getAllImages collection most likely contains absolute paths to the /posts/1.jpg files, which the join method also considers absolute from the root, so it does not add a prefix to them, but treats as is, try deleting the first character, from the file

     before_destroy :remove_file def remove_file getAllImages.each do |file| File.delete(Rails.root.join('public', file[1..-1])) end end 
    • thanks a lot, helped! - Igor Lobanov