0

I am using rails 5 API and I am using paperclip to upload user's avatar. I have an index page of all users where I have to list all the users. I need a way to display all the users avatar

@user.avatar.url can be used to get one user's avatar.

I could use

@users.each do |user|
    @avatar.push(user.avatar.url)
end

Is there another way? Also, can I save the URL into an image field using a callback like after_save or after_update during user sign up?

DroidNoob
  • 1,123
  • 9
  • 21

1 Answers1

1

Just push them in a hash

@avatars = {} 
@users.each{ |user| { @avatars[user.id] = user.avatar.url }

Now you can access users avatar using

@avatars[user_id] # This will return you the avatar
Deepak Mahakale
  • 22,834
  • 10
  • 68
  • 88