Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails omniauth facebook:如何使用活动存储保存头像?_Ruby On Rails_Omniauth_Omniauth Facebook_Rails Activestorage - Fatal编程技术网

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)
当你说“它们都不起作用”时,你是什么意思?你看到错误了吗?