Rails Paperclip result array index -
i try method avatar assets. user.rb code:
def get_avatar(size) if self.assets.length != 0 self.assets.each |asset| if asset.avatar asset.photo.url(size) end end else default_avatar(size) end end
in view:
user.get_avatar(:small) it should work in view have this:
<img alt="jpeg", photo file size: 121649, photo updated at: "2013 08 20 07:51:42", user id: 109, active: false, avatar: true>]" src="/images/[#<asset id: 176, created_at: "2013-08-20 07:51:43", updated_at: "2013-08-20 07:51:43", photo_file_name: "user_2.jpg", photo_content_type: "image/jpeg", photo_file_size: 121649, photo_updated_at: "2013-08-20 07:51:42", user_id: 109, active: false, avatar: true>]"> it doesn't take photo.url(size) whole array index.
when do:
rails.logger.fatal "check_me #{asset.photo.url(size)}" it return photo url. still doesn't work.
when :
self.assets.first.photo.url(size) it works. difference?
in get_avatar function, loop assets array, when asset's url, don't return it, function returns array itself. add return when url.
def get_avatar(size) if self.assets.length != 0 self.assets.each |asset| if asset.avatar return asset.photo.url(size) end end else default_avatar(size) end end hope helps.
Comments
Post a Comment