Rails Gravatar Helper Method
Asked Answered
H

2

5

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

Horsehair answered 16/1, 2013 at 15:31 Comment(0)
U
20
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.

Uropygium answered 16/1, 2013 at 19:9 Comment(0)
A
1

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%>
Asymptote answered 13/3, 2016 at 21:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.