Gravatar in ruby-on-rails 3.2

1

Following the railscast tutorial to add an image of the site I found an information lagged with rails updates. The code to create a default image in the case of absence of registration in the gravatar site does not work as in the code below suggests. I've tried to exchange "images" for "assets", does anyone have any idea that can solve the problem? follow the code:

module ApplicationHelper
  def avatar_url(user)
    default_url = "#{root_url}assets/default.jpg"
    gravatar_id = Digest::MD5::hexdigest(user.email).downcase
    "http://gravatar.com/avatar/#{gravatar_id}.png?s=48&d=#{CGI.escape(default_url)}"
  end
end

Here is the View code that calls the image:

<%= image_tag avatar_url(f) %>

Follow the link in the broken image that looks like this:

link

    
asked by anonymous 10.02.2014 / 21:36

1 answer

1

Gravatar is very simple, put the line below in your helper, 'app / helpers / application_helper.rb':

  def avatar_url(user)
    if user.url_image.present?
      user.url_image
    else
      gravatar_id = Digest::MD5.hexdigest(user.email.downcase)
      "http://gravatar.com/avatar/#{gravatar_id}.png?s=48"
    end
  end

And in 'app / views / home / index.html', call the helper by passing the current user as a parameter:

Logado como <%= avatar_url(current_user) %> <%= current_user.email %>. Não é você?

The method code is very simple, first it checks if there is any image url in the 'url_image' field that you have previously placed, otherwise it will use the user's email as an id to fetch the image that is stored in Gravatar , the 's = 48' parameter at the end is to set the size that the image will scale.

Source: link

    
10.02.2014 / 21:40