0

Im trying to get image based on a condition, for example a moderated image or the "main" profile image and pass a size param to carrierwave so I can have 1 method to load several image sizes in, how would one do this correctly?

My Photo model has,

  • :moderated,
  • :main attribute

to set an image moderated or set image to be the main profile picture, to display in search results. Unable to figure out how to get image based on condition.

  # Get avatar in correct size
  # Display or return default image
  # my problem: how to pass size to the carrierwave method

  def get_avatar(id, size)

      @photo_main = Photo.where(:attachable_id => id, :moderated => true, :approved => true, :main => true).first
      @photo = Photo.where(:attachable_id => id, :moderated => true, :approved => true).first


    if @photo_main
      image_tag @photo_main.file.url(:img_122x145) rescue nil
    else
      image_tag @photo.file.url(:img_122x145)
    end
  rescue
    image_tag ("/assets/avatars/img_#{size}.png")
  end
4

1 回答 1

1

In your model

has_attached_file :file,
 :style => {:small => "100x100#"}, # 100x100 any size
 ...

In Helper

image_tag(@photo.file.url(:small,false), :alt => "any text" )
于 2012-09-26T11:36:14.327 回答