Hello, I’m a web craftsman with a passion for the modern web. I build web applications and play with social services and communities.
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:
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(“email@example.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
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”
Leave a Reply