What is the point of using the :url option with paperclip? The :path option does in fact change the location where the file is saved, but the :url option doesn't seem to do a thing. It only works when it points to a publicly accessible file location. At that point, the url is already accessible to anyone. If I change the url so that it doesn't match the path, it does not work. As far as I can tell, it does not create any routes either way. Is there something I am missing here. What is the point of this option? It seems overly confusing to let someone specify a :url without actually creating a route.
I found this post useful in understanding the difference between :path and :url.
:path sets the directory in your application where the file is stored.
:url sets the url that users can use to access the image.
You are right, paperclip does not create a route for you. However, the :url option does give you the ability to select which (existing) route your users can use to download a specific image.
:path and :url usually go hand in hand. If you stick to the paperclip :default_url the path is already configured for you. Just hit the url and everything will work fine.
Changing the file location
In this example I am rendering a users avatar:
<%= image_tag #user.avatar.url %>
Now, lets say that you wanted to change the location that images are stored, you can add the following code to your model:
has_attached_file :avatar,
:path => "public/system/:class/:id/:filename"
However, the image will not render successfully. This is because the new path, where your images are stored, does not match the :default_url. Therefore, you will also need to specify a new url:
has_attached_file :avatar,
:path => "public/system/:class/:id/:filename"
:url => "/system/:class/:id/:basename.:extension"
Now the image url matches the location that the file is stored on your server and the image renders successfully.
Path vs URL
To summarize, :url tells paperclip where abouts on the server to look for an image. :path tells paperclip where to upload an image, when creating or updating a record.
Both :path and :url should point to the same location, in order to render an image successfully.
Related
I use paperclip to upload a users avatar. The image is stored correctly in the /public directory. However I cant figure out how I can get the image displayed. I played with the :url and :path settings for about an hour and cant match them in a way the image will be displayed in the browser.
There is always a 'images/localhost' in the GET-requests path that I can not get rid of.
Here is my code:
user.rb
class User < ActiveRecord::Base
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "missing.png",
:url => ':class/:id/:style.:extension',
:path => ':url'
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
end
The path in the html-tag looked up by the request looks like this:
<img alt="Original" src="/images/localhost/users/1/original.png?1405249849" />
The correct request which returns the image would be
http://localhost:3000/users/1/original.png?1405248482.
How can I get the request match the correct file-system-path. Or: what is wrong with my settings?
I didnt change the application.rb or the development.rb
Best regards,
Kev
you should take url or path helpers of paperclip wherever possible. So to show the image use:
<%= image_tag #user.avatar.url(:medium) %>
Then:
the image url isn't a file system path. Depending on the storage you use in paperclip, images can reside in different places, see understanding storage of the paperclip gem.
If you use file storage, the files are store somewhere like
public/system/users/avatar/000/000/013/small/my_pic.png
Timestamp
What you're seeing is Paperclip's timestamp - the last time the file / object was updated.
Although I can't find any official reference to this number, it basically allows you to determine which files you're dealing with. According to the question referenced above, it's apparently there for if you want to ensure your visitors see the latest version of the file (IE never gets stored in the cache)
I'm not sure why there is a disparity between your stored image & your path. However, I would say the path is correct; you just need to be able to
--
Bottom line - if your image shows on the page, I don't think there's any systemic issue with your path; if it doesn't show on the page, can you provide logs / reference to the error?
This post, https://stackoverflow.com/a/26222093/1949363, which pointed to http://www.bwigg.com/2009/10/paperclip-customizing-paths-and-urls/ was the answer for me. I was having issues only in my test environment but I believe the fix should work in other environments as well.
Try the following settings:
:path => "public/system/:class/:id/:filename",
:url => "/system/:class/:id/:basename.:extension"
Makes loading custom images, after downloading put a watermark. Paperclip by default puts the files in a folder called styles, such as original, thumb, medium, etc.
I want to hide the original file that is uploaded without a watermark, and leave the original size is available but only with a watermark.
Remove loadable file is not an option, they are needed for the archive.
I want a file kept in the same place and at the same time was not available unauthorized user? For example, a site administrator could view these files, and users could not.
Can cancan restrict access if someone will turn to the original file, the direct link?
try but I'm not sure
has_attached_file :avatar, {
:url => "/system/:hash.:extension",
:hash_secret => "longSecretString"
}
I use
Paperclip.interpolates :maybe_public do |attachment, style|
style == :original ? "private" : "public"
end
has_attached_file :image, :path => ":rails_root/:maybe_public/..."
And it's worked
So I am using paperclip to save images.
My problem is, that I have to store them out of the public-path.
When I try something like this:
:url => "users_pictures/:id/:basename.:extension",
:path => ":rails_root/assets/users_pictures/:id/:basename.:extension"
I get an error message, that the image isn't precompiled. If I remove :url and :path, everything works fine. So it is not a matter of my syntax in my view. I was thinking about access, but public has 710 and this assets-folder has 750. Do I have to modify my development/production.rb maybe ?
try this
:url => "users_pictures/:id/:basename.:extension",
:path => ":rails_root/public/assets/users_pictures/:id/:basename.:extension"
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 :path argument. So all paperclip needs to know to find the image again is:
The host name for your s3 bucket
The relevant information to re-construct the path - in this case, that looks like :prefix, :basename, :style, and :extension.
Most of that information is in your database. Presumably :basename comes from the original filename somehow, which ought to be saved in the image_file_name field of the model to which the image is attached. :style depends on which image size you're looking up at runtime. :extension can be determined from the image_content_type attribute.
:prefix is a little bit trickier. In fact, I worry that your interpolation rule will break your image lookup, because of the Date.today it 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.
i've been using Paperclip, when deleting is done, it is just deleting the attachments, but the images saved in the file system is not being deleted.can anybody plz tell how to delete the images from the file system using paperclip itself.
the code i've been using is
has_attached_file :image_o_filename,
:styles => {
:tn => ["100x100#",:jpg ,:name => :tn],
:w => ["640x480>",:jpg, :name => :w],
:l_tn => ["200x150#",:jpg, :name => :l_tn]
},
:path => "/places/hotels/:image_pre_path/images/:basename_:style.:extension"
when i delete the photos from the view, images are getting deleted from the database but not from the file system with the above path, images remain without getting deleted. what might be the reason ??
and no errors generated in console also..
Paperclip has a very nice built in delete method, I was stuck on a very similar problem and was very well informed in this solution:
I created a method inside the controller called delete_image then from the view I could call this method.
def delete_image
#user.image.destroy
end
I had the image attached to a user instance and this worked perfect, don't forget to define your method in the routes file in your config/routes.
Good luck also you can look up more information here:
See: Rails Paperclip how to delete attachment?