Hello, I’m a web craftsman with a passion for the modern web. I build web applications and play with social services and communities.

January 20, 2010 at 10:26 pm

Gravatar (globally recognized avatar) is an Avatar Service from Automattic, the company behind WordPress. It allows users to upload an avatar which is attached to an email address. This way developers all over the world can integrate “Gravatars” into their web services. All they need is a users email address. For the end-user this has the advantage that they have only one place for maintaining their avatars. Any change to their gravatar will immediately have an effect on all gravatar-enabled web services.

So how to implement Gravatar in your Ruby on Rails project?

One way would be to generate the URL to the gravatar manually.

Such a URL looks like this:

http://www.gravatar.com/avatar/3b3be63a4c2a439b013787725dfce802.jpg

The long combination of numbers and letters is a MD5 hash of the users email address.
So the Rails Code in your View could look like this:

<%= image_tag “http://www.gravatar.com/avatar/” + MD5::md5(“user@email.com”.downcase) + “.jpg” %>

Or since you are using Ruby why not go straight for a Rails Gem? “Gravtastic” does a good job here.

Just install the Gem and use

is_gravtastic!

in your User Model.

After that you have a new attribute “gravatar_url” available to any instance of “User”.

2 responses to “Use Gravatar with Ruby on Rails”

  1. slamet nurhadi says:

    I used gravatar from http://github.com/woods/gravatar-plugin
    but I puts code
    =>NoMethod,
    I am frustated

  2. Jamie says:

    Thanks for bringing me here .Its my first visit on your site Lani.I aywals do this steps. But for not regular visitor Because some times Akismet turned there comments to Spam too so i just resuce there comments from Spam Folder.BTW Nice Tips.Cheers.Amit Shaw recently posted..

Leave a Reply