简体   繁体   中英

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.

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.?

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.

Here is how I do it:
Add an image field in your User model:

rails generate migration addImageToUsers image:string

Add it attr_accessible list.

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.

You can inspect your auth hash to study different size of user image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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