Reputation: 153
I'm working through the Michael Hartl's Ruby on Rails Tutorial, and I've added code to display a user's Gravatar image. But it doesn't display.
This is my users helper
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 this is my show.html.erb
<% provide(:title, @user.name) %>
<div class="row">
<aside class="col-md-4">
<section class="user_info">
<h1>
<%= gravatar_for @user %>
<%= @user.name %>
</h1>
</section>
</aside>
</div>
This is the code when i inspect the element
<img alt="humber" class="gravatar" src="https://secure.gravatar.com/avatar/8e92292186fbb306e253b08d0f3eb993">
humber
Upvotes: 2
Views: 2441
Reputation: 1
In your custom.scss file comment out or remove the lines:
img {
display: none;
}
Upvotes: 0
Reputation: 3241
As of 2018 Michael's helper function gravatar_for
has the URL variable defined as:
gravatar_url = "https://secure.gravatar.com/gravatar/#{gravatar_id}"
However Gravatar's documentation instructs you to use:
gravatar_url = "https://www.gravatar.com/avatar/#{gravatar_id}"
The former did not work for me I was getting (404) and the image was appearing broken. The latter worked fine for me.
Upvotes: 0
Reputation: 371
I am working through this course currently (Feb 2018) using the online version of the Ruby on Rails Tutorial (Rails 5) book. I found the error to be a left over code in:
/sample_app/app/assets/stylesheets/custom.scss
/* hide all images */
img {
display: none;
}
Removing that allowed the gravatar image to display correctly for me
Upvotes: 0
Reputation: 758
I've solved it!
The tutorial directs you to download the image using curl; but this can results in a corrupt file. Why? When the book was written, the file on the website was a '.png' and today it is a '.svg'. The name has also changed from rails.png to rails-logo.svg.
By following the guide, you download a blank file and save it as a .png, and then rails gives you the middle finger.
Upvotes: 1
Reputation: 1065
I had the same problem, and tried all the solutions given this far (well, the first three anyway) but was left scratching my head. Then I removed the 'secure' from the URL and if worked just fine-- or at least for a link not associated with a gravatar. I mean, i was expecting the default gravatar image to show, and it did!
(I am also following Michael Hartl's course.)
<% hex_digest = Digest::MD5::hexdigest('[email protected]') %>
<% gravatar_url = "https://gravatar.com/avatar/#{hex_digest}" %>
<%= image_tag(gravatar_url, alt: 'gravatar image') %>
Upvotes: 0
Reputation: 101
Probably you did one of exercises in tutorial and wrote custom class to hide images
img {
display: none;
}
Upvotes: 10
Reputation: 41
Hmmm.... I tried adding an image tag and added the hash to it to see if it renders(please remove this and use gravatar_for once it works). It did, so here are the steps I did to improve the gravatar_for method. and I hope it helps.
Upvotes: 1
Reputation: 4115
your code is correct, maybe the user that you are using has no email, or the email don't have an image in gravatar. I would suggest that you validate if the user have an email
def gravatar_for(user)
if user.email?
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")
else
image_tag("/img/avatar_default.png", alt: user.name, class: "gravatar")
end
end
Upvotes: 0
Reputation: 3398
The email is fetching correctly? I have a method that does something similar, with a default url as param (in case the user email is not registered in gravatar. Check if it can help you:
def avatar
default_url = "http://pcdoctorti.com.br/wp-content/plugins/all-in-one-seo-pack/images/default-user-image.png"
gravatar_id = Digest::MD5::hexdigest(self.email).downcase
"http://gravatar.com/avatar/#{gravatar_id}.png?d=#{CGI.escape(default_url)}"
end
Ps: this is an instance method in User model
Upvotes: 0