House of Rails

Andrew House

Crazy Easy Gravatars

Gravtastic saves a ton of headaches when wanting a gravatar.
In 4 lines of code (seriously 4) I had a gravatar up and running on my app. Here’s how.

Getting Started

Keep in mind, gravtastic relies on Devise to work. So all of the instructions are to be used after Devise is setup.
Go ahead and add the gem gravtastic to your Gemfile.

Gemfilelink
1
gem 'gravtastic'


As always, make sure to bundle after installing a gem.

Terminallink
1
bundle


Implementation

Now we need to make sure our model uses gravtastic. Your file may be different depending on what you named your devise model. I’m going to name mine User and generate the code on that premise.

app/models/user.rblink
1
2
3
4
5
class User < ActiveRecord::Base
  include Gravtastic
  gravtastic
  ...
end


With that gravtastic has been able to hook into your devise model. Hooray! The last job is rendering the gravatar to the view.
I’m using Haml, so the html syntax may be weird. If you’re using Erb, just replace the = with <%= and end the line with %>

app/views/whicheverview.html.hamllink
1
=image_tag current_user.gravatar_url


That’s it!
Now a gravatar should be rendering on your page. If you want to adjust the size you can pass a size option on gravtastic in the model. The specifics of different options to use can be found on the Gravtastic Github Page