Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 973
Erin Walker Avatar asked Oct 22 '22 07:10

Erin Walker


1 Answers

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.

like image 155
kiddorails Avatar answered Oct 25 '22 22:10

kiddorails