0
votes

I was reading past Stack posts on this issue, but I was still unsuccessful in getting default profile images to work.

In my app/models folder, I have a file called "profile.rb" that looks like this:

class Profile < ActiveRecord::Base
  belongs_to :user

  # From Paperclip GitHub README
  has_attached_file :avatar, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.jpg"
  validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/
end

From what I understand, it's basically saying that styles is some sort of a placeholder for medium and thumb. So "thumb" and "medium" are supposed to be folders inside of assets/images. The default_url states that the default image will be found in :styles which is in the "thumb" or "medium" folder. I made a folder called "thumb" in my images directory and then placed the "missing.jpg" file in it. In my show.html.erb I have the line

<%= image_tag @user.profile.avatar.url %>

which works in showing the image when a user has uploaded an image, but shows up as "missing" if they don't upload an image.

I also noticed that in public/profiles/avatars/000/000/004 directory that there were 3 folders called "medium," "original," and "thumb." I assumed that this is where I needed to place my "missing.jpg," but that didn't work either so I'm a bit confused as to what to do.

According to this stack post, Paperclip is looking for images in the public directory so I am assuming this is the right place, but I still don't understand why it won't work.

1

1 Answers

0
votes

your missing image must be in a public folder or you can keep it in app/assets/images folder, since it has to be available globally and if, in future you drop your avatar table, the missing image will be gone.

after keeping your missing image in images folder.

either you can give default_url as:

default_url: "/assets/icon/missing.jpg"

or you can put a condition in your views,

<% if @user.avatar? %>
    <%= image_tag @user.profile.avatar.url(:medium)  %>    
<% else %>
    <%= image_tag "/assets/icon/missing.jpg" %>
<% end %>