Rails and Paperclip, default_url not working - ruby-on-rails

I'm using paperclip for uploading profile pictures. When someone does not upload an image I want a default image to be assigned to the user instead.
I'm using this line of code:
has_attached_file :avatar,
styles: { medium: "300x300>", thumb: "100x100>" },
default_url: "assets/images/:style/male.jpg"
But my browser inspector gives me this error:
http://localhost:3000/assets/images/original/male.jpg 404 (Not Found)
I've tried writing:
default_url: "assets/images/:style/male.jpg"
default_url: "images/:style/male.jpg"
default_url: ":style/male.jpg"
default_url: "male.jpg"
default_url: "assets/images/male.jpg"
The image lies in the following places:
/assets/images/male.jpg
/assets/images/medium/male.jpg
/assets/images/thumb/male.jpg
/assets/images/original/male.jpg

Ommit the /images/ instead do this /assets/male.jpg or /assets/original/male.jpg
Hope that help you out.

Related

Paperclip image upload in rails

I am using paperclip for image uploading and in my model I have specified styles to store the images with various sizes, I have imagemagick installed, here are the codes
has_attached_file :image,
styles: { medium: '300x300>', thumb: '40x40>' },
path: 'images/:id/image/:basename.:extension',
default_url: 'default.png'
validates_attachment :image, content_type: { content_type: %w[image/jpeg image/png] }
It doesn't seems to store images with these specified styles, It only stores image with default style. Do I miss anything here?. Thanks in advance
I did a mistake by overriding the paperclip default path without :style specified. So it stores only the default size. The processed image (by ImageMagick) does not store as I didn't mention at override path.

Setting up cloudinary and paperclip

I used the paperclip gem to upload images onto my app. Now I'm trying to deploy to Heroku and want to use Cloudinary to host the images. I followed the documentation and added my cloudinary.yml file into my config directory. Even locally when I add this my images disappear
this works locally:
has_attached_file :avatar, styles: { medium: "300x300", thumb: "100x100" }
Adding the commands for cloudinary makes all my images disappear:
has_attached_file :avatar, styles: { medium: "300x300", thumb: "100x100" },:storage => :cloudinary,
:path => ':id/:style/:filename'
If I try to push to heroku it also doesn't work.

Rails 5 - download a paperclip attachment

In the app I am building to learn RoR, I want to automatically download an attached pdf. Attached with Paperclip. Then to get the pdf properties using pdfinfo.
In my annotation.rb model I attach using
has_attached_file :file,
styles: { large: "600x600>", medium: "500x500>", thumb: "150x150#" },
default_url: "/images/:style/missing.png"
In my AnnotationsController
require 'pdfinfo'
pdfinfo = Pdfinfo.new('#annotation.file.path')
page_count = pdfinfo.page_count
this throws an error
Pdfinfo::CommandFailed in AnnotationsController#pdf pdfinfo -f 0 -l -1
-enc UTF-8 #annotation.file.path
What does this error mean and how can I get the file to be read? I learned about downloading using send_file, but have not succeeded.
You are passing a literal string to the new method. You need to remove the quotes.
require 'pdfinfo'
pdfinfo = Pdfinfo.new(#annotation.file.path)
page_count = pdfinfo.page_count

Ruby/Paperclip: How to get missing.png to work? [duplicate]

This question already has an answer here:
Rails and Paperclip, default_url not working
(1 answer)
Closed 3 years ago.
I am creating user avatars. I am having trouble getting the "missing.png" to show up. For users who decided not to upload a profile picture, I have created a default image to be displayed. Below is what I got so far. Please show me what I am doing wrong.
user.rb
has_attached_file :avatar, styles: { large: "800x800>", medium: "300x300>", thumb: "50x50>" }, default_url: "/images/:style/missing.png"
validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/
posts/index.html.erb (Shows all posts by all users, for the moment)
<%= link_to(image_tag(post.user.avatar.url(:medium)), user_path(post.user.username)) %>
users/show.html.erb (This is the user's profile page)
<%= image_tag #user.avatar.url(:medium) %>
"missing.png"
Images: app/assets/images/missing.png
Thumb: app/assets/images/thumb/missing.png
Medium: app/assets/images/medium/missing.png
Large: app/assets/images/large/missing.png
What am I doing wrong?
Instead of:
default_url: "/images/:style/missing.png"
Try:
default_url: "/:style/missing.png"

Ruby on rails gem paperclip How to generate missing image

I uploaded a missing.png to public/images/ folder. But the the missing.png not showing correctly.
The website requires images/small/missing.png
So I guest I should generate the missing.png for thumb small medium large size.
What should I do?
Update 1:
I manually create a folder public/images/small and put missing.png inside the small folder. The website shows the missing.png. But What's is the correct way to generate all size of missing.png?
Try to add this in your model:
has_attached_file :image, styles: { :small => "150x150>", medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"

Resources