Rails paperclip resizing not working - ruby-on-rails

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.

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.

Adding custom "styles" to Paperclip gem

Right now I have this
has_attached_file :cover_image,
styles: { square: "200x200#", small:"400x400>",medium:"500x600" }
I want to have this:
has_attached_file :cover_image,
styles: { thumb: "50x50#", square: "200x200#", small: "400x400>", medium: "500x600" }
When I add the "thumb" key I get a 404 error. Is there a way to create a new "style"?
Seems like there is no issue in your code.
You might be getting error while fetching previous record's thumb, which is not generated yet. Try create previous record thumbnail also and your error will gone.
Run below command in console.
rake paperclip:refresh CLASS=User ## change User with model name.
More information you can find here.

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"

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:

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