Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

default profile picture pic helper

I am stuck on a helper method for rendering a default profile picture if one is not set on sign up. Let say I have a helper method like so:

def blank_image (user)
  base_image = ""
  if @user.image.empty?
    base_image
  else
    @user.image
  end
end

what I need is the url to point base_image to so I know where to save this image. I am guessing assets/images but I don't know how to "spell out" the image location if that makes any sense. and also how can I implement this in my view. Thanks in advance.


1 Answers

Something like this?

def blank_image.url(user)
  if @user.image.empty?
    "/assets/default_photo.jpg"
  else
    @user.image.url
  end
end

The images on the assets, once they are precompiled, they will be available from /assets (same with stylesheets and javascripts), the "/images" path is removed.


Based on your new comment, the best solution may be to provide a default_url for Carrierwave, if you check the documentation, you can do something like:

class MyUploader < CarrierWave::Uploader::Base

  version :thumb do
    process resize_to_fill: [280, 280]
  end

  version :small_thumb, :from_version => :thumb do
    process resize_to_fill: [20, 20]
  end   

  def default_url
    "/assets/default_images/" + [version_name, "default.png"].compact.join('_')
  end
end

And then on your /app/assets/images/default_images/ you should add the photos create the file default.png which is the default image, thumb_default.png which is the version :thumb, and finally small_thumb_default.png which is the version :small_thumb.

notice that both :thumb and :small_thumb is an example in case you process different image versions for the image uploaded.

like image 192
rorra Avatar answered Nov 23 '25 05:11

rorra