Reputation: 16724
I have looked at the following, but they aren't clear, particularly the reference to DataMapper and gem dependencies.
All I want as an outcome is to be able to take my @user.email value that is in a |do| loop and display a gravatar where the identicon is set to "y" -- in other words, those cute seemingly random drawings!
But when I look at what is available, it isn't clear what to do -- particularly the references to DataMapper and gem dependencies.
http://github.com/chrislloyd/gravtastic/tree/master
I am playing around with this, but I wanted to get feedback from others before diving too deep!
http://www.thechrisoshow.com/2008/10/27/adding-gravatars-to-your-rails-apps
I installed woods gravatar plugin:
http://github.com/woods/gravatar-plugin/tree/master which is the same as the one referred below...however, I get an error when I type in:
<%= gravatar_for @user %>
The error is:
undefined method `gravatar_for' for #<ActionView::Base:0x474ddf4>
Upvotes: 6
Views: 5296
Reputation: 19636
You need to MD5 hash the email address and then put it into a gravatar URL. That will give you the image URL. Below is an example of how to do it.
http://www.gravatar.com/avatar/ md5(email) ?s=128&d=identicon&r=PG
If you want those random drawings that appear, you can use an MD5 hash to get them. You could hash the key value in a loop and obtain a list that way.
Upvotes: 1
Reputation: 582
I use https://github.com/sinisterchipmunk/gravatar it works well, I am only using the basics but it can do cashing and advanced options.
It is also simple to use:
Gravatar.new(email).image_url
for the identicons you could add wavatar as follows
Gravatar.new(email).image_url + '?d=wavatar'
Upvotes: 1
Reputation: 12402
Put this code in your ApplicationHelper so that gravatar_for
is available in all views.
def gravatar_for email, options = {}
options = {:alt => 'avatar', :class => 'avatar', :size => 80}.merge! options
id = Digest::MD5::hexdigest email.strip.downcase
url = 'http://www.gravatar.com/avatar/' + id + '.jpg?s=' + options[:size].to_s
options.delete :size
image_tag url, options
end
In views:
<%= gravatar_for 'my@mail' %>
<%= gravatar_for 'my@mail', :size => 48 %>
<%= gravatar_for 'my@mail', :size => 32, :class => 'img-class', :alt => 'me' %>
I refined slant's solution. Following Gravatar guidelines, e-mails should be trimmed and lowercased before hashing. Also, it seems require 'digest'
isn't needed (tested on Rails 3).
Upvotes: 13
Reputation: 935
Not to repeat too much, but instead to give a more detailed answer:
As Sam152 said, you must create an MD5 hash from the user's email address which is then used in a GET request to the gravatar server.
The easiest way to gain access to MD5 hashes is through Digest, part of the ActionPack (inside ActionView) gem. Place the following in 'config/environment.rb':
require 'digest'
Now you only need to use the following where you wish to display the user's gravatar:
image_tag("http://www.gravatar.com/avatar.php?gravatar_id=#{Digest::MD5::hexdigest(@user.email)}", :alt => 'Avatar', :class => 'avatar')
This requires no additional gems and you can create a helper as needed if all you require is pulling in the user's gravatar.
Upvotes: 8
Reputation: 1088
There's a Gravatar Rails plugin that can be found here:
http://gravatarplugin.rubyforge.org/
Install the plugin like this:
ruby script/plugin install svn://rubyforge.org//var/svn/gravatarplugin/plugins/gravatar
After installing the plugin, if your model responds to an 'email' method, this tag will show the Gravatar:
<%= gravatar_for @user %>
Upvotes: 1