Gravatar - setting custom default avatar - avatar not shown

走远了吗. 提交于 2019-12-11 11:46:36

问题


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


回答1:


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.




回答2:


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.




回答3:


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}"???



来源:https://stackoverflow.com/questions/21763944/gravatar-setting-custom-default-avatar-avatar-not-shown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!