Gravatar - setting custom default avatar - avatar

2019-08-12 02:38发布

I am on development environment on localhost:3000

I followed railscasts episode on Gravatar. I am trying to set my custom avatar image that is located in assets/images.

The code I use is this:

  def avatar_url(user)
    gravatar_id = Digest::MD5.hexdigest(user.email.downcase)
    default_url = "#{root_url}assets/AwesomeAvatar.png"
    "http://gravatar.com/avatar/#{gravatar_id}.png?s=100&r=g&d=#{CGI.escape(default_url)}"
  end

The avatar does not load. When I try to save the image there is an error: "Failed - No file".

I changed default_url to:

    default_url = "http://localhost:3000/assets/AwesomeAvatar.png"

When I go to this url i see the avatar's image but I still get the same error and the avatar does not load up. I also tried setting d as: d="http://localhost:3000/assets/AwesomeAvatar.png" and d=#{CGI.escape("http://localhost:3000/assets/AwesomeAvatar.png")} but no luck having the avatar load either.

Any clue whats wrong?

Thanks

3条回答
唯我独甜
2楼-- · 2019-08-12 03:14

I should comment on your question but my reputation does not allow me. You try with "gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}"???

查看更多
Viruses.
3楼-- · 2019-08-12 03:21

Gravatar's site says the default image you specify "MUST be publicly available (e.g. cannot be on an intranet, on a local development machine, behind HTTP Auth or some other firewall etc). Default images are passed through a security scan to avoid malicious content."

The url you are using for your default image is hosted in your development environment on localhost, so that will fail. To make it work you need to host the default image somewhere public and use that public URL for the default.

查看更多
仙女界的扛把子
4楼-- · 2019-08-12 03:22

It looks like your url is wrong, try:

gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}"

For reference this is my Users Helper:

module UsersHelper

  # Returns the Gravatar (http://gravatar.com/) for the given user.
  def gravatar_for(user, options = { size: 50 })
    gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
    size = options[:size]
    gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}"
    image_tag(gravatar_url, alt: user.name, class: "gravatar")
  end

end

You can add options[:default] = image_tag("Yourimage.png") to change the default picture.

查看更多
登录 后发表回答