Linking to an image in production using paperclip - ruby-on-rails

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

Related

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 change file system default storage RoR

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) %>

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

Paperclip not showing image even though URL/images in correct places

Using Paperclip to attach avatars to User profiles for my rails application. I followed the instructions on the paperclip github to initialize and attach to my app.
I have an image in the public/images/medium/missing.png and for both cases (when I upload or when I fallback on the default) I get no image. I've checked my directory and there is an image where it says it is looking but does not grab it. Additionally when I have tried uploading images, I know the image is uploaded correctly because when calling the User in rails console shows all the information properly attached.
I am calling the image in my view like:
<%= image_tag(#blog.user.avatar.url(":medium"), :class => "image-circle avatar") %>
my Paperclip declaration in the User model looks like the following:
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100#"}, :default_url => "/public/images/:style/missing.png"
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
Really not sure what is going on. The route errors that appear when I inspect the improperly loaded image point directly to the image in my local server. And the fact that it can't grab either the missing or the uploaded file also has me at a loss. Any help would be super appreciated!!
And for good measure the output when I examine a user with an uploaded avatar:
avatar_file_name: "11390219_10206114805925816_6595348111261743639_n.j...", avatar_content_type: "image/jpeg", avatar_file_size: 101926, avatar_updated_at: "2015-07-10 18:51:44">
Thanks in advance!
EDIT
This is the URL that is providing the 404 error:
http://localhost:3000/images/medium/missing.png
while in my local directory it goes "root/public/images/medium/missing.png"
not sure how its not grabbing it, unless I am just missing something really obvious somewhere. (i tried hard routing the public in there as well, but to no avail).
EDIT
There is the possibility that you're simply not serving the static assets, add:
config.serve_static_assets = true
to your development.rb
ORIGINAL POST
In you application.rb ( or an environment specific file ), you should have a config.paperclip_defaults = { ... }, here is the link in the docs: https://github.com/thoughtbot/paperclip#defaults
Here is an example one, using fog:
config.paperclip_defaults = {
:storage => :fog,
:fog_credentials => {
:provider => "Local",
:local_root => "#{Rails.root}/public"
},
:fog_directory => "",
:fog_host => "localhost:3000"
}
Do you have something like that in your application? I just tested on an app of mine, and I was able to upload an image, but not to see any without the paperclip_defaults hash. Also, don't forget to restart your app after you update the config files. I hope this helps!
Have you tried killing the quotes around the image style? Around :medium?
<%= image_tag(#blog.user.avatar.url(:medium), :class => "image-circle avatar") %>
As show here in the Paperclip Docs:
https://github.com/thoughtbot/paperclip#show-view
Same problem, solved it by reinstalling imagemagic with brew
Details: In case you're imagemagic, try to update the imagemagic and if it asks you to link it,
Try with this:
brew link --overwrite imagemagick
It worked for me. Hope its helpful

Paperclip: Isn't creating the thumb folder?

I'm using the paperclip gem (3.0.4) which suddenly stopped working as it is no longer creating the thumb folder, only original. Because of this my code can't find the image that was uploaded and errors out.
In my model I have:
has_attached_file :image,
:whiny => false,
:path => ":rails_root/public/system/:attachment/:id/:style/:filename",
:url => "/system/:attachment/:id/:style/:filename",
:styles => { :thumb => "150x100#" }
This code hasn't changed, and when I attempt to render my view I get the following error:
/public/system/images/52/thumb/2011-03-16_07-49-52_412.jpg not found
Looking in my file system I have, /public/system/images/52/original/2011-03-16_07-49-52_412.jpg
Could this be an issue with ImageMagick?
Any idea what is causing this and how to resolve it to ensure the thumb folder is being created? Thanks for your time and assistance.

Resources