Using Gravatars in Rails
Gravatars are great ways to show user images in a Rails application. There are gems for adding this functionality but we can roll this for ourselves without adding a new dependency. The best part is, it only takes 7 lines of code.
First, let's create a helper class so that we can use this throughout an application:
First, let's create a helper class so that we can use this throughout an application:
module UsersHelper def gravatar_for(user) gravatar_id = Digest::MD5::hexdigest(user.email.downcase) gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}" image_tag(gravatar_url, alt: user.name, class: "gravatar") end end
All we need is an image_tag that uses a url for its first argument. The url is comprised of the default gravatar base url "https://secure.gravatar.com/avatar/" with a downcase version of the email address appended. I would suggest that a gem is not needed for something so simple.