6

Recently I installed Paperclip gem and I am struggling to make the default image work on my system, I put the image file inside assets/images/pic.png.

This is code in my model User:

has_attached_file :pic,
  :styles => { :medium => "300x300>", :thumb => "100x100>" },
  :default_url => 'missing_:avatar.png'
  #:default_url => 'assets/images/avatar.png'

has_attached_file :attach

This is code inside my AddPicPaperClip migration:

def self.up
  add_column :users, :pic_file_name,    :string
  add_column :users, :pic_content_type, :string
  add_column :users, :pic_file_size,    :integer
  add_column :users, :pic_updated_at,   :datetime
  add_attachment :users, :pic
end

def self.down
  remove_column :users, :pic_file_name
  remove_column :users, :pic_content_type
  remove_column :users, :pic_file_size
  remove_column :users, :pic_updated_at
  remove_attachment :users, :pic
end

In order to display the image in my show I have this code:

<%= image_tag @user.pic.url(:medium) %>

Could you help me find a solution, in order to show a default picture before a user inputs his own profile picture?

Thiago Belem
  • 7,732
  • 5
  • 43
  • 64
Michael
  • 111
  • 1
  • 1
  • 6
  • 1
    Might this be a dup of [this question](http://stackoverflow.com/questions/9646549/default-url-in-paperclip-broke-with-asset-pipeline-upgrade)? – Ben Mar 24 '14 at 01:32
  • Thank you very much, i fixed the problem thanks to your post. – Michael Mar 24 '14 at 02:42

2 Answers2

11

I got the error in ermenkoff's solution.

Error: AbsoluteAssetPathError

It was fixed by removing "/assets/" from the url.

:default_url => ":style/missing_avatar.jpg"

Images are stored in:

app/assets/images/medium/missing_avatar.jpg
app/assets/images/thumbs/missing_avatar.jpg
mecyborg
  • 407
  • 3
  • 12
  • 1
    This isn't working for me. I need to access my "whale.jpg" file in my assets/images directory. I am typically showing my profile picture as <%= image_tag (at)user.profile.avatar.url %> but how would that work for default? I tried <%= image_tag (at)user.profile.avatar.url, :default_url => "whale.jpg" %> with and without the :style tag, but it didn't work. NOTE: I had to use "at" because stack wouldn't let me post the symbol. – Jubl May 01 '16 at 19:53
  • Hi @Jubl, Can you post the error message ? And make sure the images are in subfolders too according the sizes. – mecyborg May 02 '16 at 06:16
  • There is a small error: the folder needs to be called /thumb/ (or "thumbnail", or whatever name you gave your style), NOT /thumbs/. Automatic pluralisation doesn't apply here. – Sprachprofi Apr 18 '17 at 18:30
2
:default_url => "/assets/:style/missing_avatar.jpg"

:style is substituted with medium or thumb, depending on what you've requested. You should put your defalut images in:

app/assets/images/medium/missing_avatar.jpg
app/assets/images/thumbs/missing_avatar.jpg
viktor_vangel
  • 894
  • 9
  • 16
  • There is a small error: the folder needs to be called /thumb/ (or "thumbnail", or whatever name you gave your style), NOT /thumbs/. Automatic pluralisation doesn't apply here. – Sprachprofi Apr 18 '17 at 18:31