Ruby on Rails Gravatar Simple Integration

Want Gravatar installation and integration in four lines of code? Easy! Now that's Gravtastic!

taelor
3/10/2009

Line 1 – Installation

Gravtastic is a Gravatar gem created by Chris Lloyd.


1 sudo gem install gravtastic



Line 2 – Configuration

I like the choice of creating a gem instead of a plugin, makes it usable by more than just Rails.


1 config.gem gravtastic



Line 3 – Integration

Notice I’m using DataMapper, works just fine, no need to tweak anything. I do have a field called email that gravtastic picks up on, but you could specify another field name if you wanted.


1 class Comment
2 include DataMapper::Resource
3
4 is_gravtastic
5
6 #rest of code
7 end



Line 4 – Implementation

Finally just need to display the link to the image next to whoever is commenting.


1 <%= image_tag(comment.gravatar_url(:size=>25)) %>




4 lines for auto avatar icons? Now that’s slightly coded!

Other solutions

gravatar_tag

 
3/11/2009
 

Comments


D41d8cd98f00b204e9800998ecf8427e hasham2 said...
Mar 10, 2009

I have also created a simple gravatar plugin http://hasham2.blogspot.com/2007/07/using-gravatar-plugin-to-embed-avatars.html

Eb221232ec43ef9be46b0839ec1e7a18 Ang said...
Apr 21, 2009

Hi, looks like you are using DataMapper....but the title says using Ruby on Rails....do you have an example where not using DataMapper for ORM?

D41d8cd98f00b204e9800998ecf8427e taelor said...
Apr 24, 2009

well, I'm still using rails, just using the more supreme (in my humble opinion) DataMapper for this blog. But I think the use is exactly the same, all you have to do is just say 'is_gravtastic' in your model with an 'email' column in your table. If you got any other questions, lemme know.

Eb221232ec43ef9be46b0839ec1e7a18 Ang said...
Jan 01, 2010

is there a way to just gravatar_url('email@domain.com')? I haven't created the user models yet but want to set up the views and pass dummy emails

7379d033523d1963a64f38fe88fa97e1 satya said...
Mar 03, 2010

Useful one. Will try it out !

New Comment