Last Updated: February 25, 2016
·
581
· danielpclark

Rails Gravatar Helper Method

Gravatar is a great avatar per email service. All you need to do is convert the email address to a md5 checksum. Then plug that into the avatar image url and you have your avatar image.

You should consider the options you want to set for the image. See the documentation for that: https://en.gravatar.com/site/implement/images/

I've set my options as dimmensions, a G rating, and blank if no image available. I've chosen to only have image size and border as the parameters for my Gravatar Helper Method. Here's that method:

def gravatar(email, size, border)
  "<img src=\"http://www.gravatar.com/avatar/#{
  Digest::MD5.new.update(
    email.to_s.strip.downcase
  ).hexdigest
  }?d=blank&s=#{size}&r=g\" style=\"height:#{size}px;width:#{size}px;border:#{border}px\">".html_safe
end

Be sure to follow my blog! http://6ftdan.com

  • Daniel P. Clark