我使用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
我做错了什么?
答案 0 :(得分:1)
Andrew,编写here:方法attr_accessible使该属性可用于批量分配。
你应该这样说:
attr_accessible :email, :password, :password_confirmation, :remember_me, :first_name