我正在使用Omniauth嘗試在我的Web應用程序上註冊/登錄用戶。 這是我AuthenticationsController#Create
方法內發生的事情:NoMethodError - 未定義的方法`user'for nil:NilClass
認證控制器:
class AuthenticationsController < InheritedResources::Base
def create
omniauth = request.env['omniauth.auth']
authentication = Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid'])
if authentication
flash[:success] = "Signed in successfully"
sign_in_and_redirect(authentication.user)
elsif current_user
token = omniauth['credentials'].token
secret = omniauth['credentials'].secret
current_user.authentications.create!(:provider => omniauth['provider'], :uid => omniauth['uid'], :token => token, :secret => token_secret)
flash[:success] = "Authentication successful"
redirect_to authentications_url
else
user = User.new
user.apply_omniauth(omniauth)
if user.save!
flash[:success] = "Account created"
sign_in(authentication.user)
else
session[:omniauth] = omniauth.except('extra')
redirect_to '/signup'
end
end
end
我本來sign_in(:user, authentication.user)
,但它給了我的說法的錯誤,所以我改成了剛剛在上面的代碼had sign_in(authentication.user)
。但是,現在我得到NoMethodError - undefined method 'user' for nil:NilClass
。
該控制器的正確「def創建」應該是什麼樣子?我試圖按照Railscast for omniauth – user2159586 2013-04-11 05:37:40