【问题标题】:Rails: Setting up a gravatar image for user profileRails:为用户个人资料设置 gravatar 图像
【发布时间】:2014-03-07 21:39:49
【问题描述】:

我正在通过Michael Hartl's Ruby on Rails 教程向用户个人资料添加 Gravatar 图像。

这是我的代码:

#app/views/users/show.html.erb

<% provide(:title, @user.name) %>

<h1>
  <%= gravatar_for @user %>
  <%= @user.name %>
</h1>     

还有帮手:

#app/helpers/users_helper.rb

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

然后我使用 update_attributes 来更新数据库中的用户:

$ rails console
>> user = User.first

>> user.update_attributes(name: "Example User",
?> email: "user@myapplication.com",
?> password: "password",
?> password_confirmation: "password")
=> true

所以在那之后当我访问用户个人资料的显示页面时,我只有原始的 gravatar 图片,但没有与用户电子邮件关联的自定义 gravatar。

有什么问题吗?

谢谢,

【问题讨论】:

  • 电子邮件user@myapplication.com 是否有gravatar.com 的帐户?如果是新帐户,则需要几分钟才能完成。
  • 不,这只是一个例子,我使用了我的真实电子邮件地址,在 gravatar 上有一张自定义照片...

标签: ruby-on-rails devise profile gravatar


【解决方案1】:

你提供的邮箱必须有gravatar账号,这样你才能渲染自定义图片,如果没有在gravtar注册,它会为你提供默认图片,试试注册邮箱

【讨论】:

    猜你喜欢
    • 2015-11-15
    • 1970-01-01
    • 2017-05-30
    • 2012-01-02
    • 1970-01-01
    • 2011-01-25
    • 2018-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多