Ruby on rails omniauth facebook:如何使用活动存储保存头像?
我使用Ruby on rails omniauth facebook:如何使用活动存储保存头像?,ruby-on-rails,omniauth,omniauth-facebook,rails-activestorage,Ruby On Rails,Omniauth,Omniauth Facebook,Rails Activestorage,我使用omniauth facebookgem对用户进行身份验证。然而,我不知道如何将化身存储到活动存储器中。 有人能给我提建议吗 我试过这两种方法。但它们都不起作用 user.avatar = auth.info.image 都不是 downloaded_image = open(auth.info.image) user.avatar.attach(io: downloaded_image, filename: 'avatar.jpg', content_type: 'image/jpg'
omniauth facebook
gem对用户进行身份验证。然而,我不知道如何将化身存储到活动存储器中。
有人能给我提建议吗
我试过这两种方法。但它们都不起作用
user.avatar = auth.info.image
都不是
downloaded_image = open(auth.info.image)
user.avatar.attach(io: downloaded_image, filename: 'avatar.jpg', content_type: 'image/jpg')
谢谢大家!
auth.info.image
返回一个字符串,该字符串是指向化身的url。如果你只是想显示头像,我建议你使用一个字符串字段并存储url
如果确实要下载并保存图像,请尝试以下代码:
# in order to use the open() method with urls
require 'open-uri'
# open the link
downloaded_image = open(auth.info.image)
# upload via ActiveStorage
# be careful here! the type may be png or other type!
user.avatar.attach(io: downloaded_image, filename: 'avatar.jpg', content_type: downloaded_image.content_type)
当你说“它们都不起作用”时,你是什么意思?你看到错误了吗?