Michael
Michael

Reputation: 111

How to set default image in paperclip ruby on rails

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?

Upvotes: 6

Views: 12387

Answers (2)

mecyborg
mecyborg

Reputation: 407

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

Upvotes: 11

viktor_vangel
viktor_vangel

Reputation: 1012

: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

Upvotes: 2

Related Questions