Gravatars on Rails

Patrick showed me a very cool site: gravatar.com.  At this site, you can upload an image of yourself (or whatever) and tie it to an email address.  Then, any time you post a comment on a Gravatar-enabled blog, your image will show up next to your comment.This would be a great feature to add to greenisus, so I visited the implementation page at gravatar.com.  Unfortunately, there was no help for using gravatars with a Ruby on Rails site.  So, I coded a solution. First,  go into application_helper.rb for your rails app, and add the following: 

require 'digest/md5'

  Then, add the following helper method:  

def gravatar_url_for(email, options = {})      url_for({ :gravatar_id => Digest::MD5.hexdigest(email), :host => 'www.gravatar.com',                     :protocol => 'http://', :only_path => false, :controller => 'avatar.php'                }.merge(options))  end

 Now, you can use Gravatar URLs in your views like this:  

# plain old gravatar url  <%= gravatar_url_for 'greenisus@gmail.com' %>    # gravatar url with a rating threshold   <%= gravatar_url_for 'greenisus@gmail.com', { :rating => 'R' } %>    # show the avatar   <%= image_tag(gravatar_url_for 'greenisus@gmail.com')%>   # show the avatar with size specified, in case it's served slowly  <%= image_tag(gravatar_url_for('greenisus@gmail.com'), { :width => 80, :height => 80 }) %>    # link the avatar to some/url  <%= link_to(image_tag(gravatar_url_for 'greenisus@gmail.com'), 'some/url')%> 

 Enjoy! And look out for gravatars at greenisus soon!

3 Comments

  1. Safeguy says:

    how do i look?

  2. [...] Flash-Tutorial MultiMediaTechnologyAudio visual weekend in ParisAugmented Reality fürs iPhone MMT08 BlogsGoltergaul: C++ Sockets – Webserver (refactored)Hubert Hölzl: Gravata für Ruby on Rails [...]