我遵循Railscasts#235和#236設置使用omniauth創建用戶身份驗證。 http://railscasts.com/episodes/235-omniauth-part-1 http://railscasts.com/episodes/236-omniauth-part-2Rails 3:創建身份驗證時更新用戶屬性
我對所謂的用戶模型2個布爾屬性:facebok_share和:twitter_share,我想,當一個新的設置爲true身份驗證已創建。
我有這個爲我工作,當我創建一個新的用戶,但如果一個現有的用戶添加一個身份驗證我不能得到布爾更新爲true。
當調用apply_omniauth(omniauth)時,它會在我的用戶模型中設置self.facebook_share = true或self.twitter_share = true。
我試着添加一個名爲apply_share的新方法,它根據提供者更改布爾值,並且我試圖調用current_user.apply_share(omniauth),但在數據庫中沒有任何事情發生。
我在做什麼錯?謝謝!
##認證控制器
class AuthenticationsController < ApplicationController
def index
@title = "Authentications"
@authentications = current_user.authentications if current_user
end
def create
# creates omniauth hash and looks for an previously established authentication
omniauth = request.env["omniauth.auth"]
authentication = Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid'])
# if previous authentication found, sign in user
if authentication
flash[:notice] = "Signed in successfully"
sign_in_and_redirect(:user, authentication.user)
# for users already signed in (current_user), create a new authentication for the user
elsif current_user
current_user.apply_share(omniauth)
current_user.authentications.create(:provider => omniauth['provider'], :uid => omniauth['uid'], :token => (omniauth['credentials']['token'] rescue nil),
:secret => (omniauth['credentials']['secret'] rescue nil))
flash[:notice] = "authentications successful"
redirect_to authentications_url
# new user is created and authentications are built through apply_omniauth(omniauth)
else
user = User.new
user.apply_omniauth(omniauth)
if user.save
flash[:notice] = "Signed in successfully"
sign_in_and_redirect(:user, user)
# if validations fail to save user, redirects to new user registration page
# new twitter authentications redirect so user can enter their password
else
session[:omniauth] = omniauth
redirect_to new_user_registration_url
end
end
end
def destroy
@authentication = current_user.authentications.find(params[:id])
@authentication.destroy
flash[:notice] = "Successfully destroyed authentication."
redirect_to authentications_url
end
end
## user model
# set share booleans to true depending on 'provider' type
def apply_share(omniauth)
case omniauth['provider']
when 'facebook'
self.facebook_share = true
when 'twitter'
self.twitter_share = true
end
end
# from authentications controller, new user split into type of provider
def apply_omniauth(omniauth)
case omniauth['provider']
when 'facebook'
self.apply_facebook(omniauth)
when 'twitter'
self.apply_twitter(omniauth)
end
# builds authentication with provider, uid, token, and secret
authentications.build(hash_from_omniauth(omniauth))
end
protected
# sets new user attributes from facebook
def apply_facebook(omniauth)
self.name = omniauth['user_info']['name']
self.email = omniauth['user_info']['email'] if email.blank?
self.facebook_share = true
end
# sets new user attributes from twitter
def apply_twitter(omniauth)
if (extra = omniauth['extra']['user_hash'] rescue false)
# Example fetching extra data. Needs migration to User model:
# self.firstname = (extra['name'] rescue '')
self.name = (extra['name'] rescue '')
self.bio = (extra['description'] rescue '')
end
self.twitter_share = true
end
# set authentication attributes to those from 'omniauth' hash
def hash_from_omniauth(omniauth)
{
:provider => omniauth['provider'],
:uid => omniauth['uid'],
:token => (omniauth['credentials']['token'] rescue nil),
:secret => (omniauth['credentials']['secret'] rescue nil)
}
end
end
## new methid with :before add => :apply_share
def apply_share(authentication)
case authentication['provider']
when 'facebook'
self.facebook_share = true
when 'twitter'
self.twitter_share = true
end
self.save
end
調用current_user.save確實可行,但我試圖用before_add調用設置你的想法,我無法讓它工作。我需要區分身份驗證提供者是'臉譜'還是'推特',所以我試過這個,但它不工作,有什麼想法? (也與上述的語法粘貼)DEF apply_share(認證) 情況下認證[ '提供商'] 時的Facebook' self.facebook_share =真 當'Twitter的 self.twitter_share =真 端 self.save 端 –
嘗試使用authentication.provider而不是身份驗證['provider'] – Kelend
感謝所有工作 –