views:

461

answers:

3

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 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 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>
+1  A: 

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 and md5 hash to get them. You could hash the key value in a loop and obtain a list that way.

Sam152
How do I add the d=identicon when I use the gravatar plugin?
Angela
A: 

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 %>
Terry
Hi, I installed it and I get an error:undefined method `gravatar_for' for #<ActionView::Base:0x47c08e0>
Angela
Is there something I am supposed to do to "enable" it? I notice that it creates a plugin called gravatar-plugin...do I need to rename it? I had to do that for restful_authentication.
Angela
Did you restart your server?
Terry
ah...that did it! How do I change the plugin so that the gravatar functions as an img_link?
Angela
probably wrap the link tag around the gravatar_for tag, so something like:<%= link_to((gravatar_for @user), :controller => :your_controller, :action => :your_action %>
Terry
Cool! Last quick queston: can I add d=identicon into the URL, and how?
Angela
I'd love to help you, but these questions are pretty mundane. Have you done any research on your own?If you wanted to change the gravatar_url, you could look at the source code generating the gravatar img url (it's in vendor/plugins/gravatar).If you wanted to change the actual link, you could look at the Rails documentation for link_to.
Terry
I did -- I didn't see an OPTION for gravatar_url in the actual helper. Someone helped me, thanks.
Angela
A: 

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.

slant