Topic: Gravatar Helper Method

Hi
I have followed this railscast on Gravatars

i have this helper method

module ApplicationHelper
  def avatar_url(user)
    gravatar_id = Digest::MD5.hexdigest(user.email.downcase)
    "http://gravatar.com/avatar/#{gravatar_id}.png?s=200"
  end
end

and this in the view

<%= image_tag avatar_url(user) %> 

How do i add options so when i specify a size it changes the s=200 to the size i specified?

Thanks

Re: Gravatar Helper Method

http://blog.jerodsanto.net/2008/07/pass … by-method/

I'm no pro, but I'd try this:

module ApplicationHelper
  def avatar_url(user, options = {})

    opts = {
      :size   => 200
    }.merge options

    gravatar_id = Digest::MD5.hexdigest(user.email.downcase)

    "http://gravatar.com/avatar/#{gravatar_id}.png?s=#{size}"
  end
end
Learning rails, one day at a time.

http://keilmiller.com

Re: Gravatar Helper Method

JUst define a helper method in the corresponding helper module, for example UsersHelper as follows:

module UsersHelper

  # Returns the Gravatar (http://gravatar.com/) for the given user.
  def gravatar_for(user)
    gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
    gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}"
    image_tag(gravatar_url, alt: user.name, class: "gravatar")
  end
end

And call the helper in your view:

<h1>
  <%= gravatar_for @user %>
  <%= @user.name %>
</h1>

Last edited by Javix (2013-01-22 07:38:58)

Re: Gravatar Helper Method

Thanks for your help guys, worked like a charm.