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?
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.
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:
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
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.
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
: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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With