Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Gravatar Helper Method

I have gone through this railscast on Gravatars and i now have the below helper method in my application helper.

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 i have this in my view

<%= image_tag avatar_url(user) %>

how can i modify the helper so it will accept a size option that changes the s=200 to the size specified?

Thanks

like image 632
Ollie2619 Avatar asked Jan 16 '13 15:01

Ollie2619


2 Answers

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

Then call:

<%= image_tag avatar_url(user, 200) %>

You can also check Michael Hartl's guide.

like image 116
Tom Burgess Avatar answered Oct 25 '22 19:10

Tom Burgess


In the application_helper.rb file under the app/helpers folder, add the following method:

# app/helpers/application_helper.rb

def gravatar_for(user, options = { size: 200})
  gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
  size = options[:size]
  gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}?s=#{size}"
  image_tag(gravatar_url, alt: user.username, class: "img-circle")
end

Then call it at the show.html.erb:

<%= gravatar_for @user, size: 200%>
like image 1
Rod Chfat Avatar answered Oct 25 '22 18:10

Rod Chfat