There is a user model:

class User < ActiveRecord::Base has_many :posts, dependent: :destroy has_many :comments, dependent: :destroy has_attached_file :avatar, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png" validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/ devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable end 

Code in views:

 <p> <strong>Avatar:</strong> <br> <%= image_tag @user.avatar.url(:medium) %> </p> 

Did everything according to the documentation https://github.com/thoughtbot/paperclip If the user does not have an avatar, then it is not displayed. In the / assets / images folder there is an image of missing.png, but I get at the output enter image description here

It turns out an error in the example developer

How to solve a problem?

1 answer 1

The fact is that you cannot do with a missing.png file, you must create your own missing file for each style (provided that you do not override the mechanism for finding missing files). Those. if you use the: medium style, by default PaperClip will look for it in the assets / images / medium / missing.png way, if you want to use the: thumb style, you also need to prepare an image for assets / images / thumb / missing.png.

The styles are different, their size is different, therefore the images are also different. To navigate where PaperClip expects to see the image, simply create a user without an avatar in the Rails console and look at the URL of the image

 user = User.last user.avatar.url(:medium) "/images/medium/missing.png"