私のレールアプリでは、次の手順を実行しています。
ユーザーはログインする必要があります -> ユーザーが承認を作成します -> ユーザーが作成されます
プロセスの最終段階でアプリがつまずいているようです。
承認を正常に作成します。
Authorization Load (0.9ms) SELECT "authorizations".* FROM "authorizations" WHERE "authorizations"."provider" = 'facebook' AND "authorizations"."uid" = '2259711' LIMIT 1
Authorization attributes hash: {"id"=>1, "uid"=>"2259711", "provider"=>"facebook", "user_id"=>1, "created_at"=>Wed, 02 May 2012 06:06:13 UTC +00:00, "updated_at"=>Wed, 02 May 2012 06:06:13 UTC +00:00}
しかし、ユーザー作成ステップでチョークします。
User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Completed 500 Internal Server Error in 26ms
RuntimeError (Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id):
app/controllers/sessions_controller.rb:21:in `create'
セッション コントローラーの 21 行目は次のとおりです。session[:user_id] = @auth.user.id
ただし、適切なIDでユーザーオブジェクトを明確に初期化したため、これは意味がありません...
このステップを管理するコードは次のとおりです。
セッション#コントローラの作成
def create
# create the auth hash here
auth_hash = request.env['omniauth.auth']
if session[:user_id]
# Means our user is signed in. Add the authorization to the user
User.find(session[:user_id]).add_provider(auth_hash)
redirect_back_or User.find(session[:user_id])
# render :text => "You can now login using #{auth_hash["provider"].capitalize} too!"
else
# Log him in or sign him up
@auth = Authorization.find_or_create(auth_hash)
# Create the session
logger.debug "Person attributes hash: #{@auth.attributes.inspect}"
session[:user_id] = @auth.user.id
# render :text => "Welcome #{auth.user.name}!"
sign_in @auth.user
redirect_back_or @auth.user
end
end
承認 find_or_create モデル メソッド
def self.find_or_create(auth_hash)
unless auth = find_by_provider_and_uid(auth_hash["provider"], auth_hash["uid"])
user = User.create :name => auth_hash["info"]["name"], :email => auth_hash["info"]["email"]
logger.debug "Within Authorization find_or_create: Person attributes hash: #{user.attributes.inspect}"
auth = create :user => user, :provider => auth_hash["provider"], :uid => auth_hash["uid"]
end
auth
end
アップデート
これを試すと、logger.debug "User attributes hash: #{@auth.user.attributes.inspect}"
上記のセッションコントローラーで次のエラーが発生します。
NoMethodError (undefined method `attributes' for nil:NilClass):
app/controllers/sessions_controller.rb:21:in `create'
ユーザー オブジェクトが nil であることを意味します。認証メソッド内で user.create を呼び出すので、これは当てはまりませんfind_or_create
...