使用Paperclip(v4.2.0)保存的附件在模型被销毁时未能从磁盘中删除,其他人是否也遇到了这个问题?一切都按预期运行,但文件并没有从磁盘中删除。非常感谢任何帮助或想法!
模型:
模型:
class Attachment < ActiveRecord::Base
belongs_to :article
has_attached_file :file, { :preserve_files => "false" }
do_not_validate_attachment_file_type :file
end
class Article < ActiveRecord::Base
belongs_to :topic
belongs_to :subtopic
belongs_to :author
has_many :attachments, :dependent => :destroy
accepts_nested_attributes_for :attachments, allow_destroy: true, reject_if: lambda { |a| a[:file].blank? }
validates :topic_id, presence: true
validates :title, presence: true, length: { maximum: 16 }
validates :subtitle, length: { maximum: 20 }
validates :content, presence: true
end
文章控制器中的销毁操作:
def destroy
@article = Article.find(params[:id])
begin
# first delete the attachments from disk
@article.attachments.each do |a|
a.file.destroy
end
@article.destroy
rescue
flash[:danger] = "Unable to delete article"
else
flash[:success] = "Article deleted"
end
redirect_to admin_articles_url
end
@article.attachments.destroy
吗? - Rajesh Omanakuttan