1
votes

I am using cloudinary to store user avatars in my rails 4 application. I also have a placeholder image in my image assets. I wanted to know that if a user hasnt uploaded his avatar, how do i load it from the localhost.

As of now i have to add checks as

- if user.avatar.present?
  = cl_image_tag(user.avatar.filename, width: 46, height: 46)
- else
  = image_tag 'default.png', style: 'width:46px; height:46px;'

i can specify a default image as

= cl_image_tag(user.avatar.filename, width: 46, height: 46, default: 'default.png')

but the default image has to be stored on cloudinary. I dont want to store it on cloudinary since cloudinary charges for data transfer. hence, i have stored 'default.png' is stored in assets. 'cl_image_tag' is a helper provided by cloudinary to load images from there.

1
store the default image assets/images folder.Sampat Badhe
@Sampat its stored there. the issue is i want to only write 'user.avatar.filename' and it display the image from assets if no image is uploaded else should retrieve from cloudinary.prasad.surase

1 Answers

3
votes

I solved the problem as below

Firstly, the default images need to be saved on cloudinary. eg

<%= cl_image_tag("non_existing_id.png", width: 100, height: 100, default_image: "avatar.png") %>

if you dont want to load default image from cloudinary but from our localhost, add the below code in a partial and use this partial.

- if user.avatar.present?
  = cl_image_tag(user.avatar.filename, width: 46, height: 46)
- else
  = image_tag 'noPic_80.png', style: 'width:46px; height:46px;'