Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 380
Michael Avatar asked Mar 24 '14 01:03

Michael


People also ask

What is Paperclip in Ruby?

Paperclip is a cleanly abstracted Ruby library that reduces the complexity of file uploading and processing. Using Paperclip with an external storage service such as Amazon S3 or Rackspace CloudFiles allows you to scale your application’s files and codebase independently.

How to create a migration from Terminal in rails?

There is a rails command that makes this possible from your terminal. rails generate migration add_avatar_to_users That will create a new migration in db/migrate. Open it up and paste the below code:

Should I use Paperclip to build my next web application?

Conclusion You might want to consider Paperclip as you build your next web application. It has a great team supporting it. To explore other features not covered in this tutorial, check Paperclip's GitHub page. martijn broeders

What is Paperclip in ActiveRecord?

Paperclip is an easy file attachment library for ActiveRecord. It treats files like model attributes. This means they aren’t saved to their final locations, nor are they deleted if set to nil, until ActiveRecord::Base#save is called.


2 Answers

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
like image 94
mecyborg Avatar answered Nov 15 '22 00:11

mecyborg


: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
like image 29
viktor_vangel Avatar answered Nov 15 '22 00:11

viktor_vangel