Im referring to this app link as a tutorial. After looking at it. Im curious to understand how the program (client or server)can accesse images. Since the program doesn’t seem to be explicitely saving image urls in the server’s database once they are uploaded to Amazon S3?
After implementing this app, I dont see a column for image_urls or path in the database. But the program magically loads images in the client!!
Something is going on under the hood, what is that something?
Here is the Photo model method:
Paperclip.interpolates :prefix do |attachment, style|
"#{attachment.instance.takenby}/#{Date.today.to_s }/#
{attachment.instance.image_file_name}"
end
has_attached_file :image,
:path => ":prefix/:style/:basename.:extension",
:styles => { :thumbnail => "57x57", :original => "300x300" },
:storage => :s3,
:s3_credentials => S3_CREDENTIALS
validates :image,:presence => true
validates :lat, :lng,:presence => true,:numericality => true
It’s right there in the options for
has_attached_file. You’ve told it how to construct the path for the image (which corresponds to its filename on s3) in the:pathargument. So all paperclip needs to know to find the image again is::prefix,:basename,:style, and:extension.Most of that information is in your database. Presumably
:basenamecomes from the original filename somehow, which ought to be saved in theimage_file_namefield of the model to which the image is attached.:styledepends on which image size you’re looking up at runtime.:extensioncan be determined from theimage_content_typeattribute.:prefixis a little bit trickier. In fact, I worry that your interpolation rule will break your image lookup, because of theDate.todayit uses to construct the prefix. Do you have any images more than one day old? If so, do they still work? I worry that when the image is uploaded, it will have a filename containing that day’s date, and then when you go to find them in the future you’ll reconstruct the url using the new current date, and wind up with a 403 error.TL;DR paperclip constructs the image url when it uploads the images to s3, and reconstructs them later from the same parameters.