当行: PaperClip不执行validates_attachment时:
after_post_process :extract_text是活跃的。如果我将其注释掉,validates_attachment将按需要执行。文档指出,验证是在post_process之前完成的。
我使用的是Rails 4.2.3、Ruby2.0、PaperClip 4.3.0。
class Document < ActiveRecord::Base
validates :title, presence: true, length: { minimum: 5 }
has_attached_file :fdoc,
:default_url => "/images/:style/missing.png"
validates_attachment :fdoc,
:presence => true,
:content_type => { :content_type => "application/pdf" },
:file_name => { :matches => [/pdf\Z/] }
after_post_process :extract_text
def extract_text
reader = PDF::Reader.new(fdoc.queued_for_write[:original].path)
full_text = ""
reader.pages.each do |page|
full_text << page.text
end
self.text = full_text
end
end更新:
我添加了一个begin/ prevent块,以防止extract_text函数崩溃,如果上传不是PDF,并且验证执行正确。
因此,文档是不正确的。验证在post_process事件之后执行。
发布于 2015-07-30 19:44:04
根据回形针,README.md,after_post_process不会开火,如果self.valid?都是假的。然而,我发现无论是什么,after_post_process都是被调用的,所以我添加了:
after_post_process :my_method
def my_method
if self.valid?
# execute code
end
endhttps://stackoverflow.com/questions/31665588
复制相似问题