通过rails誓言导入facebook个人资料图片

时间:2015-01-14 14:08:59

标签: ruby-on-rails ruby facebook facebook-graph-api

我正在使用誓言facebook gem来验证我的应用中的用户:https://omr-altijdheerlijk.herokuapp.com/这项工作正在进行中。

我还在我的应用中为用户帐户添加了名称和图片字段。名称字段将自动填入Facebook的用户名。现在我也试图通过Facebook的认证将用户的个人资料图片从facebook导入到rails中的帐户。这不起作用,请参阅下面的代码:

我的user.rb文件

class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable, :omniauthable, :omniauth_providers => [:facebook]

has_many :pins

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

has_attached_file :image, :styles => { :medium => "300x300>", :square => "200x200>", :thumb => "100x100>" }, :default_url => "/images/:style/default.jpg"
end

我的应用程序控制器

class ApplicationController < ActionController::Base
 # Prevent CSRF attacks by raising an exception.
 # For APIs, you may want to use :null_session instead.
before_action :authenticate_user!

protect_from_forgery with: :exception
before_filter :configure_permitted_parameters, if: :devise_controller?

protected

  def configure_permitted_parameters
  devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:name, :image, :email, :password,    :password_confirmation, :current_password) }
  devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:name, :image, :email, :password, :password_confirmation, :current_password) }
  devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:name, :image, :email, :password, :password_confirmation, :current_password) }
  end

 end
顺便说一句,我也使用Devise(常规登录)和Paperclip,ImageMagic和Amazon S3来上传和存储图像。

有人可以帮助我做错了吗?

2 个答案:

答案 0 :(得分:0)

感谢您的帮助。看来我更进了一步。我认为facebook的图片现在正在导入。但是我的视图中还没有显示图像。我在视图中有以下代码

 <%= link_to image_tag(user.image.url(:medium)), user %>

当我查看HTML时,我看到图像的以下链接: http://s3.amazonaws.com/altijdheerlijk/users/images/000/000/019/medium/http://graph.facebook.com/10152500285363157/picture?type=square

答案 1 :(得分:0)

你应该

user.image_file_name = auth.info.name

然后在你的观点中:

<%= link_to image_tag(user.image_file_name), user %>