简体   繁体   English

使用Omniauth-Facebook Gem添加个人资料图片的最简单方法是什么?

[英]What is the simplest way to add an profile image with the Omniauth-Facebook Gem?

I'm working with the Omniauth Facebook Gem with Rails and need a profile pick on my app for each user. 我正在使用带有Rails的Omniauth Facebook Gem,需要为我的应用程序选择每个用户的个人资料。

Is it as simple as retrieveing the Name of the user from facebook with the Gem, or does it need to be uploaded to Amazon S3 servers etc.? 是否像使用Gem从facebook检索用户名一样简单,还是需要将其上传到Amazon S3服务器等?

My user model: 我的用户模型:

def self.from_omniauth(auth)
    where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user|
      user.provider = auth.provider
      user.uid = auth.uid
      user.name = auth.info.name
      user.oauth_token = auth.credentials.token
      user.oauth_expires_at = Time.at(auth.credentials.expires_at)
      user.save!
    end

Can't find a clear answer. 找不到明确的答案。

Updated: In my show page it only shows the url: 更新:在我的展示页面中,它只显示网址:

<b>IMAGE:</b> <%= @user.image %>

Thanks 谢谢

It is simple to do. 这很简单。 And you don't require to store image on your server. 而且您不需要在服务器上存储图像。 You can simply fetch it from facebook. 你可以简单地从Facebook上获取它。

Here is how I do it: 我是这样做的:
Add an image field in your User model: User模型中添加image字段:

rails generate migration addImageToUsers image:string

Add it attr_accessible list. 添加attr_accessible列表。

Then in your above method, add following listing for image: 然后在上面的方法中,为图片添加以下列表:

user.image = auth.info.image

This is the direct url of where facebook stores the image of the user. 这是facebook存储用户图像的直接网址。

You can inspect your auth hash to study different size of user image. 您可以inspect您的auth哈希来研究不同大小的用户图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM