我正在使用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