How to know if an image has been uploaded or not? - Paperclip

僤鯓⒐⒋嵵緔 提交于 2019-12-08 19:23:04

问题


do you know if is there a method to know if the image has been uploaded?

I mean, i have a Foo_Class, and this class can have an attached image, but its presence is not necessary. Is there a way to know if a particular instance of that class have the image or not?

Thanks!


回答1:


When you added Paperclip to your model you added paperclip specific rows, mine are

cover_file_name
cover_content_type
cover_file_size
cover_updated_at

Then I check whether it is nil or not

 Foo_Class.cover_file_name.nil? 



回答2:


If foo.image? returns true, then file uploaded.




回答3:


I think that the proper solution is to use the file? method.

foo.image.file?

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

using exists? will do a request to the server to check if the file is there, which can be quite slow, especially if it's on a different server or on S3.

using foo.image_file_name.nil? is probably the same as file? under the covers, but ou don't want to dependant on the implementation of paperclip, which could someday change.




回答4:


If this is in my model

has_attached_file :avatar, :styles => {:logo => "230x50>", :card_image => "180x50>"}

You can check if the image is uploaded for a user i.e @user

<%= @user.avatar.exists? %>

This will return boolean value.




回答5:


Suppose you want to check the presence of attachment(image) for the 1st row in Foo model:

image_present = Foo.first.image?

This returns true if the attachment is present.



来源:https://stackoverflow.com/questions/5593829/how-to-know-if-an-image-has-been-uploaded-or-not-paperclip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!