Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 601
Abramodj Avatar asked Apr 08 '11 10:04

Abramodj


4 Answers

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

like image 133
Quaternion Avatar answered Sep 18 '22 15:09

Quaternion


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? 
like image 34
Nick Avatar answered Sep 17 '22 15:09

Nick


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.

like image 24
tomf Avatar answered Sep 17 '22 15:09

tomf


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.

like image 20
Animesh Avatar answered Sep 19 '22 15:09

Animesh