如何知道图片是否已上传?- 回形针

Abr*_*odj 8 paperclip ruby-on-rails-3

你知道是否有方法知道图像是否已上传?

我的意思是,我有一个Foo_Class,这个类可以有一个附加的图像,但它的存在是没有必要的.有没有办法知道该类的特定实例是否有图像?

谢谢!

小智 11

如果foo.image?返回true,则上传文件.

  • 它现在是`foo.image.exists?` (7认同)

Nic*_*ick 8

当您将Paperclip添加到模型时,您添加了回形针特定行,我的是

cover_file_name
cover_content_type
cover_file_size
cover_updated_at
Run Code Online (Sandbox Code Playgroud)

然后我检查它是否为零

 Foo_Class.cover_file_name.nil? 
Run Code Online (Sandbox Code Playgroud)


tom*_*omf 6

我认为正确的解决方案是使用该file?方法.

foo.image.file?

http://rdoc.info/github/thoughtbot/paperclip/Paperclip/Attachment#file%3F-instance_method

使用存在?将向服务器发出请求以检查文件是否存在,这可能非常慢,特别是如果它位于不同的服务器或S3上.

使用foo.image_file_name.nil?可能与文件相同?在幕后,但你不想依赖回形针的实施,这可能有一天会改变.