gpt4 book ai didi

facebook - Devise和OmniAuth:如何保存Facebook数据供以后使用?

转载 作者:行者123 更新时间:2023-12-01 11:11:39 25 4
gpt4 key购买 nike

我已经使用Devise和OmniAuth(内置于Devise)成功实现了Facebook登录。现在,我需要弄清楚如何将用户名存储在数据库中,以便可以向其他用户显示他们的名称,以查看他们是否在我的应用程序中创建/编辑了记录。

我必须从某处复制了此代码,看起来它应该可以工作,但是绝对不能将名称保存在数据库中:

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
@user = User.find_for_facebook_oauth(env["omniauth.auth"], current_user)

if @user.persisted?
flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Facebook"
sign_in_and_redirect @user, :event => :authentication
else
session["devise.facebook_data"] = env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
end

class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable, :lockable and :timeoutable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable

# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me

def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
data = access_token['extra']['user_hash']
if user = User.find_by_email(data['email'])
if user.first_name != data['first_name']
user.update_attributes :first_name => data['first_name']
end
user
else # Create an user with a stub password.
User.create!(:email => data['email'],
:first_name => data['first_name'],
:password => Devise.friendly_token[0,20])
end
end

def self.new_with_session(params, session)
super.tap do |user|
if data = session['devise.facebook_data'] && session['devise.facebook_data']['extra']['user_hash']
user.email = data['email']
end
end
end
end

我究竟做错了什么?

最佳答案

Andrew,如here所述:attr_accessible方法使属性可用于批量分配。

您应该这样写:

attr_accessible :email, :password, :password_confirmation, :remember_me, :first_name

关于facebook - Devise和OmniAuth:如何保存Facebook数据供以后使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6854072/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com