paperclip: image not getting uploaded rails - ruby-on-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"

Related

Rails and Paperclip storing images in a specific path sets wrong URL

I want to store my images using the normal file storage adapter.
This is my PAPERCLIP_STORAGE_OPTS:
PAPERCLIP_STORAGE_OPTS = {
:styles => { :thumb => '170x170!#', :medium => '450x300!>', :large => '600x400!>',:desktop => '750x300!>'},
:convert_options => { :all => '-quality 100' },
:processor => [ :papercrop ],
:path => "/opt/www/myapp/images/:class/:attachment/:id_partition/:style/:filename"
}
This is my model :
class User < ActiveRecord::Base
attr_accessor :PAPERCLIP_STORAGE_OPTS
has_attached_file :user_photo, PAPERCLIP_STORAGE_OPTS_THUMB
When a user uploads a photo - it actually does store the image in the correct location on my system:
/opt/www/myapp/images/users/user_photos/000/000/050/original/picture
However when I go to show the image, like this :
<%=image_tag current_user.user_photo.url(:thumb), :height=> "30", :width=> "30" %>
The image is not found, and in my logs I see the image request at this URL:
ActionController::RoutingError (No route matches [GET] "/system/users/user_photos/000/000/050/thumb/picture"):
And the full URL created is :
https://www.myapp.com/system/users/user_photos/000/000/050/thumb/picture?1460285803 - which doesnt resolve.
How can I configure paperclip to allow my images to be stored in this particular url /opt/www/myapp/images/ and still be accessed and linked to correctly through Paperclip in my rails app?
You will have to set URL option:
for me it was:
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>", :small=>"60x60>" },
:path => ':rails_root/public/system/:class/:id/:style/:filename',
:url => '/system/:class/:id/:style/:filename'
Not sure for your case as you store images in the app folder directly so you may try(test it from console and modify it):
:path => "/opt/www/myapp/images/:class/:attachment/:id_partition/:style/:filename",
:url => '/images/:class/:attachment/:id_partition/:style/:filename'

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

Paperclip change default path and hash image names

I'm new to Rails and using Paperclip, I have it set up on my model already without issue. I just don't like the path it's generating for my images right now (:root_path/system/users/avatars/000/000/001) I really don't even understand it. How can I modify this default path for my images to something more friendly? And how can I hash the image names?
In your model, you can set the default path, styles and url as so:
has_attached_file :avatar,
:styles => { :large => "500x500>", :medium => "300x300>", :thumb => "100x100>" },
:path => ":rails_root/public/images/:id/:style/:filename",
:url => "/images/:id/:style/:filename"
you can setup the hash on the paperclip initializer file (config/initializers/paperclip_defaults.rb)
Quoting from paperclip wiki:
Paperclip::Attachment.default_options.update({
:path => ":class/:attachment/:hash/:style.:extension",
:hash_secret => "SOME_RANDOM_SECRET"
})
The :hash part is generated from :hash_secret and the pattern given by the :hash_data option, which by default is ":class/:attachment/:id/:style/:updated_at".

Getting Image Paperclip::Errors::NotIdentifiedByImageMagickError after updating to Paperclip 3.4.0

I got the following message after upgrading:
Paperclip 3.0 introduces a non-backward compatible change in your
attachment path. This will help to prevent attachment name clashes
when you have multiple attachments with the same name. If you didn't
alter your attachment's path and are using Paperclip's default, you'll
have to add :path and :url to your has_attached_file definition.
For example:
has_attached_file :avatar,
:path => ":rails_root/public/system/:attachment/:id/:style/:filename",
:url => "/system/:attachment/:id/:style/:filename"
So I did so:
post.rb:
has_attached_file :image, :styles => { :medium => "170x300>",
:thumb => "142x185>" },
:path => ":rails_root/public/system/:attachment/:id/:style/:filename",
:url => "/system/:attachment/:id/:style/:filename"
But then I saw this error message:
Image Paperclip::Errors::NotIdentifiedByImageMagickError
I even added this to environments/development.rb:
Paperclip.options[:command_path] = "/usr/bin/"
(which identify outputs /usr/bin/identify)
But still no luck.
What could be the problem?
Wow, I didn't expect this. The problem wasn't due to upgrading.
It was because the file I was uploading was named like this:
Screenshot at 2012-11-26 16:22:44.png
Weird.
The issue is in the filename.
colons are not accepted, if you remove the colon from the attachment name using gsub it'll be accepted always.

Paperclip avatars image missing if user didnt uploaded image

I am using Paperclip for uploading the Profile image in my application (rails)
My User model is having a
has_attached_file :avatar,
:url => "/:attachment/:id/:style/:basename.:extension",
:path => ":rails_root/public/:attachment/:id/:style/:basename.:extension",
:styles => { :medium => "300x300>", :small => "100x100>", :thumb => "50x50>", :micro => "30x30>" }
In my VIew If i have a profile image uploaded then its pointing correctly as
/avatars/1/thumb/iamge
BUt if the image is not there if they didn't uploaded in that case its pointing as
/avatars/thumb/missing.png which doesn't have any image.
Please give suggestions what to do if the user didnt uploaded any profile image..
You can make a default image and put it there and name it missing.png. It is like extra functionality :)

Resources