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"
Related
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"
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.
I am trying to resize my images to 256x256 ignoring aspect ratio, so I used:
has_attached_file :thumbnail, styles: { medium: "256x256!", thumb: "300x300"}
in my model, and in view:
<%= image_tag guide.thumbnail(:medium), class: "guide_thumbnail" %>
but It fails to work. After changing to:
has_attached_file :thumbnail, styles: { medium: "50x50!", thumb: "300x300"}
Nothing changes, so I guess It is using default styles? How can I change it and force to use my ones instead? Please help.
PS: After adding newly named style:
has_attached_file :thumbnail, styles: { medium: "256x256!", thumb: "300x300", hihi: "50x50!"}
It does not show any image - only its name. I checked the logs and that is the error message:
ActionController::RoutingError (No route matches [GET] "/system/guides/thumbnails/000/000/088/hihi/test300.jpg"):
What should I do?
And Additionally I am curious if setting the width of an image via css(for example width: 256px;) is a good practice?
You can change into this:
has_attached_file :thumbnail, styles: { medium: "50x50>", thumb: "300x300"}
In my opinion, you'd better use image version properly with the real version.
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:
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"