在 Rails 上获取头像 omniauth discord Ruby

get avatar omniauth discord Ruby on Rails

在我的 user.rb 中,我尝试将 link 保存到头像:

def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
user.image = auth.info.image
end

它可以与 omniauth-google-oauth2 一起使用,但是使用 discord 我只能得到图像 src 的一半。例如:

https://cdn.discordapp.com/avatars/714396517990037405/

其中 /714396517990037405/ 是用户 ID,缺少图像名称

这似乎是 omniauth-discord gem 部分的错误。他们concatenate the URL without checking whether an avatar is actually set.

但是如果你查看 Discord documentationavatar 实际上是 ?string 类型,所以它可以是 null.

一般建议:如果某些东西不起作用并且它是开源的,请查看幕后情况。很有可能您不仅会快速找到解释或解决方案,而且还会了解您在此过程中使用的东西。