Gravatar Image_tag showing a random person that I have never seen before (Ruby on rails)

116 Views Asked by At

This is my code for a profile panel that I am working on for a project. I am still new to rails and I just started to use gravatar. And when I ran the panel I get a random person on the image. I have never seen this person before, and no idea who they are. How does that happen?

 <nav class="panel">
        <p class="panel-heading">   
            Profile
        </p> 
        <p class= "panel-block"> 
            <article class="media">
                <div class="media-left">
                    <figure class="image is-64x64">
                        <%= gravatar_image_tag(current_user.email, size: 64, alt: current_user.name) %>
                    </figure>
                </div>
                <div class="media-content">
                    <div class="content">
                        <p>
                            <strong><%= current_user.name %></strong><br/>
                            <small><%= current_user.username %></small>
                        </p>
                    </div>
                </div>
            </article>
        </p>
    </nav>

When I do inspect element, I get this link for a specific avatar from gravatar. Have I been hacked? Looking forward for your help!

<img size="64" alt="fireball" src="http://gravatar.com/avatar/c3f5511d8046fc67693d50b0473c1a85" width="80" height="80">
1

There are 1 best solutions below

1
Abrar Jahin On

You Aren't getting the proper image maybe because of wrong setup! Let's fix it.

We'll start from the scratch!

Let's check the application helpers file in app/helpers/application_helpers.rb You can also follow this method by adding the following lines inside of the ApplicationHelper module:

# app/helpers/application_helpers.rb

module ApplicationHelper
  def gravatar_for(user, options = { size: 80})

    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
end

HERE inside of the def gravatar_for,

  1. gravatar_id is setting the URL from user Email id
  2. size is here to set the size of the image in link tag we need to render
  3. gravatar_url is the main URL skeleton of the Gravatar URL
  4. image_tag is defining the img tag that what this tag will contain! you can set the class here too for every gravatar image tag.

NOW, add the following tag in your code where this is needed to render!

<%= gravatar_for @user, size: 100 %>

HERE:

  1. gravatar_for is the def part we set that in helper module
  2. @user is getting the user info
  3. size: is the size we need for image url, default is 80 which is set in the helper.

Now this will work. I guess you will find helpful for every users Gravatar image setup including current user also.

Thanks for reading this. If anything else you need from Gravatar, this is also helpful by editing this slightly.

Happy Railings