繁体   English   中英

Ruby无法删除文件

[英]Ruby cannot delete file

我有这样的小的代码:

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

问题是它不会起作用。 我想产生此错误是因为保存图像后,我使用imagemagick运行了一些图像处理。 这意味着该图像已被其他进程使用。 我没有发现任何回调,我可以在图像处理后追加。 所以现在我想知道我可以代替! 例如try catch while循环? 谢谢

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'

尝试将代码更改为此:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM