Paperclip change file system default storage RoR - ruby-on-rails

I'm using paperclip to store and display images. I have change the default file system storage to:
has_attached_file :avatar,
:path => ":rails_root/upload/:rails_env/:class/:attachment/:id_partition/:style/:filename",
styles: { medium: "300x300>", thumb: "50x50>" }, default_url: "/images/:style/missing.png",
:url => "/upload/:rails_env/:class/:attachment/:id_partition/:style/:filename"
I want to store the images in the "upload" folder at the root of the application (Not in public). This part works fine.
The issue is the index and show view. I have:
<%= image_tag #user.avatar.url(:thumb) %>
Instead of showing the ":thumb" of the image I only get the file name. I don't know why!
I know the url is correct because it does get to the right image but only the name is display.
Any ideas?
Thanks in advance :)

Try <%= image_tag #user.avatar(:thumb) %>

Related

paperclip: image not getting uploaded rails

I am using paperclip gem in rails,
my user model file contains:
has_attached_file :profile_pic, styles: { medium: "300x300>", thumb: "100x100>" },
default_url: "/images/:style/missing.png",
:url => "/assets/users/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/users/:id/:style/:basename.:extension"
My image is not getting uploaded and hence not getting displayed (broken Image with "missing.png" appears).what should i do to upload my image to /assets/images path ?
and My view part look like:
<%= image_tag current_user.profile_pic.url(:thumb) %>
It might be due to various reason
1) In strong parameter you need to define :profile_pic
2) you forgot to put missing.png
3):path =>:rails_root/public/assets/users/:id/:style/:basename.:extension"
it might be wrong way to define path first :class then :style and then after :extension
:path => "images/:class/:style/:id.:extension"

Set restrictions for files in /public folder with rails and paperclip

I'm using paperclip for image storage, and i have a problem. When the file is uploaded, paperclip generates two files, a processed image with a watermark and the original file, in this case the image files are in public folder, now the question is, can i restric the url in case the user enter into it like:
localhost:3000/files/photos/image_processeds/57308cd52cb1be0846e4be9f/original/image.png
If the user enter into the link, it will be forbidden.
This is my paperclip config
has_mongoid_attached_file :image_original,
:url => "/files/:class/:attachment/:id/:style/:basename.:extension",
:path => ":rails_root/public/files/:class/:attachment/:id/:style/:basename.:extension"
and
has_mongoid_attached_file :image_processed,
processors: [:watermark],
styles: {
thumb: ['150x150', :jpg],
small: ['350x300', :jpg],
medium: ['550x500', :jpg],
original: {geometry: '60%',watermark_path: "#{Rails.root}/public/images/logo.gif", position: "Center"}
},
:url => "/files/:class/:attachment/:id/:style/:basename.:extension",
:path => ":rails_root/public/files/:class/:attachment/:id/:style/:basename.:extension"
Theres is a way to do it or maybe find a better approach for that?
The original/image.png should be watermarked I guess.
I mean the version without watermark should not exist after processing.
Did you check it?
Solved, a reliable solution is save a copy of the original image into the database and grant access to it only when they purchase the photo, otherwise they only can see the copies with watermark :)

Paperclip ignores image sizing options

So I've got the Paperclip gem setup in my project to handle cover images for a model called Story:
class Story < ActiveRecord::Base
...
has_attached_file :cover_image,
styles: { large: "700x700>", thumb: "300x300>" },
default_url: "#{Rails.root}/app/assets/images/default_image.jpg",
path:"/cover_images/:filename",
processors: [:thumbnail, :paperclip_optimizer]
I'm trying to use the thumb style for when the story is displayed in a small format, which looks like this:
<div class="image">
<%= image_tag story.cover_image.url(:thumb) %>
</div>
This is the way the Paperclip documentation says you should specify which style you want to use.
In this context, the image has the correct dimensions of 300x111px.
However, when I move to my story page, I want the large/full size version of the image:
<div class="header_image">
<%= image_tag(#story.cover_image.url(:large)) %>
Except paperclip incorrectly still uses the thumbnail version of the image:
Why isn't Paperclip sizing the images correctly?
You must specify a :style option in the path:
has_attached_file :cover_image,
styles: { large: "700x700>", thumb: "300x300>" },
default_url: "#{Rails.root}/app/assets/images/default_image.jpg",
path:"/cover_images/:filename/:style",
processors: [:thumbnail, :paperclip_optimizer]
Now Paperclip will upload different versions of the image and access them as needed:

Rails image_tag auto prefixes "/images" before #user.avatar.url instead of "/assets" in paperclip

I am using paperclip to upload user avatar.
Here is User model:
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "user.png", :path => "app/assets/images/:class/:attachment/:id/:basename_:style.:extension", :url => ":class/:attachment/:id/:basename_:style.:extension"
So image is saving in app/assets/images/user/avatar/:id/:basename_:style.:extension
But when I do
<%= image_tag #user.avatar.url %>
It shown as:
<img src="/images/users/avatars/15/99e88dc27c19d8c6163d9cd305f738be_original.jpg" alt="99e88dc27c19d8c6163d9cd305f738be original">
i.e. inserts "/images" instead of "/assets"
I double-checked, the avatar image exists in the assets/images/user/avatar/ folder
Although all other images in page are showing correctly using assets pipeline "/assets/logo-thebighashgohere.png"
NOTE: This works correctly if I manually insert image url as string
i.e.:
<%= image_tag "users/avatars/15/99e88dc27c19d8c6163d9cd305f738be_original.jpg" %>
It correctly shows as
<img src="/assets/users/avatars/15/99e88dc27c19d8c6163d9cd305f738be_original-thebighashgohere.jpg" alt="99e88dc27c19d8c6163d9cd305f738be original thebighashgohere">
I strongly recommend you to not save user-generated-content into the assets folder!
if your website is going into production mode, the assets are compiled and everything you throw in at runtime won't be catched.
stuff like that belongs into the /public directory (!)
to solve your problem
:url => ":class/:attachment/:id/:basename_:style.:extension
you tell paperclip how to generate your "url". with "path" you define where the files are stored internally, with url you control how to generate the routes. your rote is wrong, no asstes path in it.
but again - do not save those picutres into assets !
btw: i wonder if your solution is even possible in productionmode. the assetspipeline is generting files with a digest appending on it, while paperclip knows nothing about those digest it will always render a route without the digest-stamp. by that you can't call the images from the assets pipeline. so your whole concept won't work in production, but i might be wrong

Linking to an image in production using paperclip

I have a RoR app that is using paperclip for upload images, and the color-box gem to view a larger version of the image. The problem is that it woks fine in development, but when I try to take the app to production it is looking for the image in the wrong place.
It tries to find the image in the root directory and gives an error that the file does not exist at URL/system/pictures/2/large/img.png, when it should be looking for the image in URL/s12/gallery/bsd/system/pictures/2/large/img.png
I don't know how to tell the app to look for the file in URL/s12/gallery/bsd/ Any help would be appreciated, let me know if more info is needed.
Here is how I am linking to the file in the view:
<%= link_to(image_tag(#project.picture.url(:thumb)), #project.picture.url(:large), :data=> { :colorbox => true }) %>
That gets the thumbnail to show up fine, but when clicked it says there is no file in the root directory...because there isn't.
Thank you.
has_attached_file :photo, :styles => { :small => "150x150>" },
:url => "/assets/products/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/products/:id/:style/:basename.:extension"
you can set the url to change the defaults. also path is the system path where url is the url for the public

Resources