Question

I have this small code:

Dir.foreach(Rails.root.to_s + "/public/dokumente") do |item|
  next if item == '.' or item == '..'
  img = Photo.new(image: File.new(Rails.root.to_s + "/public/dokumente/" + item))
  if img.save
    File.delete(Rails.root.to_s + "/public/dokumente/" + item)
  end
end

The problem is that it wont work. I suppose this error is produced because i run some image processsing with imagemagick after the image gets saved. What means that the image is used by another process. I found no callback that i could append after the image processing. So now i would like to know what i could instead! For example a try catch while-loop? Thanks

Errno::EACCES (Permission denied - C:/Sites/sanaryapiheroku2/public/dokumente/5.
png):
  app/controllers/patients_controller.rb:28:in `delete'
  app/controllers/patients_controller.rb:28:in `block in show'
  app/controllers/patients_controller.rb:24:in `foreach'
  app/controllers/patients_controller.rb:24:in `show'
Était-ce utile?

La solution

Try changing your code to this:

Dir.foreach(Rails.root.to_s + "/public/dokumente") do |item|
  next if item == '.' or item == '..'
  file = File.new(Rails.root.to_s + "/public/dokumente/" + item)
  img = Photo.new(image: file)
  if img.save
    file.chmod(0777)
    File.delete(Rails.root.to_s + "/public/dokumente/" + item)
  end
end

http://ruby-doc.org/core-2.1.1/File.html#method-i-chmod

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top