当我通过谷歌创建新帐户时,电子邮件会存储在我的用户记录中。当我通过Twitter创建用户时,电子邮件列为空白。如果用户将他们当前的Twitter帐户与谷歌相关联,我想更新该条目。
在我的用户模型中:
def self.create_from_hash!(hash) create! do |user| user.name = hash['user_info']['name'] user.email = hash['user_info']['email'] end end
在会话控制器中:
def create auth = request.env['rack.auth'] unless @auth = Authorization.find_from_hash(auth) @auth = Authorization.create_from_hash(auth, current_user) end self.current_user = @auth.user flash[:notice] = "Welcome, #{current_user.name}." redirect_to '/' end
并在授权模型中:
def self.create_from_hash(hash, user = nil) user ||= User.create_from_hash!(hash) Authorization.create(:user => user, :uid => hash['uid'], :provider => hash['provider']) end
在添加授权方法时,如何更新该列?
答案 0 :(得分:0)
我将以下行添加到我的sessionscontroller创建操作中,这似乎解决了这个问题:
if @user && (@user.email.blank? || @user.email.nil?) @user.update_attribute(:email, request.env['rack.auth']['user_info']['email']) unless request.env['rack.auth']['user_info']['email'].nil? || @user.nil? end