0

我的Rails应用程序有fb登录,当用户通过fb登录验证,然后我得到他的电子邮件和全名,但不能得到他的个人资料图片。Rails应用程序没有采取Facebook登录fb登录后的图像

我的应用助手,

module ApplicationHelper 
def avatar_url(user) 
    if user.avatar 
     user.avatar 
    else 
      "/images/missing.png" 
    end 
end 

Omniauth_callbacks_controller.rb是,

class OmniauthCallbacksController < Devise::OmniauthCallbacksController 

def facebook 
    @user = User.from_omniauth(request.env["omniauth.auth"])  

    if @user.persisted? 
     sign_in_and_redirect @user, :event => :authentication 
     set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format? 
    else 
     session["devise.facebook_data"] = request.env["omniauth.auth"] 
     redirect_to new_user_registration_url 
    end 
end 

def google_oauth2 
@user = User.from_omniauth(request.env['omniauth.auth']) 

if @user.persisted? 
    sign_in_and_redirect @user, event: :authentication 
    set_flash_message(:notice, :success, :kind => "Google") if is_navigational_format? 
else 
    redirect_to root_path, flash: { error: 'Authentication failed!' } 
end 

我的用户模型是,

class User < ActiveRecord::Base 

    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable, :omniauthable, omniauth_providers: [:google_oauth2, :facebook] 

    validates :fullname, presence: true, length: {maximum: 40} 
    has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100#" }, :default_url => "/images/missing.png" 
    validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/ 

    def self.from_omniauth(auth) 
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user| 
     user.fullname = auth.info.name 
     user.provider = auth.provider 
     user.uid = auth.uid 
     user.email = auth.info.email 
     user.avatar = auth.info.avatar 
     user.password = Devise.friendly_token[0,20] 
    end 
    end 
end 

我从Facebook获得授权,但没有获得fb配置文件图片。

+0

<(%)= IMAGE_TAG avatar_url(CURRENT_USER)%> – MSK

回答

0
  1. 看起来像你正在使用omniauth-facebook rails gem。在facebook 授权哈希中,映像网址在auth.info.image_url中可用。在 您的示例代码,您已经使用auth.info.avatar
  2. 看起来像你正在使用回形针宝石用于存储图像。您可以查看下面的回形针维基,以便下载远程网址的图像 。 https://github.com/thoughtbot/paperclip/wiki/Attachment-downloaded-from-a-URL