メソッドを少し書き直してfind_for_facebook_oauth
、ユーザーが Facebook アカウントを現在のアカウントにマージできるようにしました。また、メソッドを変更して、新しい参加者 (ユーザーにリンクされています) を作成しました。
ただし、can't dup NilClass
誰かがメソッドを使用すると、エラーが発生することはめったにありません (常にではありません)。これは方法です:
def self.find_for_facebook_oauth(auth, token, signed_in_resource=nil, facebook_referal_id)
user = User.where(:provider => auth.provider, :uid => auth.uid.to_s).first
#new user
if !user and !userbymail
logger.info "User not found. Creating new user in user.rb"
user = User.new(
provider:auth.provider,
uid:auth.uid,
email:auth.info.email,
password:Devise.friendly_token[0,20]
)
logger.info "Received FB information"
user.skip_confirmation! #to skip the email
logger.info "Skipped the mail"
user.accepted_tos = true
#Only add participant if new user is saved
logger.info "Now saving user"
if user.save
logger.info "User saved"
#name:auth.extra.raw_info.name,
@participant = Participant.new
@participant.user_id = user.id
@participant.lastname = "Pan"
@participant.dob = Time.now - 18.years
@participant.firstname = auth.extra.raw_info.first_name || "Secret"
@participant.lastname = auth.extra.raw_info.last_name || "Secret"
if auth.extra.raw_info.gender == "male"
@participant.gender = 2
elsif auth.extra.raw_info.gender == "female"
@participant.gender = 1
else
@participant.gender = 4
end
@participant.dob = Date.strptime(auth["extra"]["raw_info"]["birthday"],'%m/%d/%Y') || Time.now - 18.years
@participant.nationality = "AQ"
どこで問題が発生するのかわかりません。Facebookから渡されたときに、何かが空である可能性はありますか? エラーを見ましたが、何が問題なのかわかりません。
app web.1 - - (facebook) Callback phase initiated.
app web.1 - - Processing by Users::OmniauthCallbacksController#facebook as HTML
app web.1 - - Parameters: {"state"=>"f357f10d5f136b28732465968ee29451af9406391ecd1144", "code"=>"AQAUePnXS7qggpjUE0gNPuPHODFOcfd6vo2zJvl4KCCrH8Mp2j4_6PS5t71R_PeHCJaFpM_ERjb9QWHqUkP6OB7WPI22Q16y1YwfwKy7IHxlz5q56DoCINMzh3MuYgKTJRFjs3XAKOo-Ncf7jigmlp1GADlBW1aB-F-5oUGpNQw1GcFVyPAnOwXD3B3wr82FO0om_z7uhXDtEDf2_RP14Vo5"}
app web.1 - - Geocoder call
app web.1 - - Italy
app web.1 - - User Load (3.3ms) SELECT "users".* FROM "users" WHERE "users"."provider" = 'facebook' AND "users"."uid" = '1503444170' LIMIT 1
app web.1 - - User Load (28.5ms) SELECT "users".* FROM "users" WHERE "users"."email" = 'app+4ss3t1sal5.1cppf6a.d25032a0cdac6d19ac26ad2c44e14cb@proxymail.facebook.com' LIMIT 1
app web.1 - - User not found. Creating new user in user.rb
app web.1 - - (44.0ms) BEGIN
app web.1 - - User Exists (2.0ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = 'app+4ss3t1sal5.1cppf6a.d25089e10cdac6d19ac26ad2c44e14cb@proxymail.facebook.com' LIMIT 1
heroku nginx - - 2.230.67.191 - - [06/Apr/2013:11:16:42 +0000] "GET /users/auth/facebook/callback?state=f357f10d5f136b28732465968ee29451af9406391ecd1144&code=AQAUePnXS7qggpjUE0gNPuPHODFOcfd6vo2zJvl4KCCrH8Mp2j4_6PS5t71R_PeHCJaFpM_ERjb9QWHqUkP6OB7WPI22Q16y1YwfwKy7IHxlz5q56DoCINMzh3MuYgKTJRFjs3XAKOo-Ncf7jigmlp1GADlBW1aB-F-5oUGpNQw1GcFVyPAnOwXD3B3wr82FO0om_z7uhXDtEDf2_RP14Vo5 HTTP/1.1" 500 763 "-" "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31" www.thestudentranking.com
heroku router - - at=info method=GET path=/users/auth/facebook/callback?state=f357f10d5f136b28732465968ee29451af9406391ecd1144&code=AQAUePnXS7qggpjUE0gNPuPHODFOcfd6vo2zJvl4KCCrH8Mp2j4_6PS5t71R_PeHCJaFpM_ERjb9QWHqUkP6OB7WPI22Q16y1YwfwKy7IHxlz5q56DoCINMzh3MuYgKTJRFjs3XAKOo-Ncf7jigmlp1GADlBW1aB-F-5oUGpNQw1GcFVyPAnOwXD3B3wr82FO0om_z7uhXDtEDf2_RP14Vo5 host=www.thestudentranking.com fwd="2.230.67.191" dyno=web.1 connect=3ms service=705ms status=500 bytes=763
app web.1 - - SQL (41.7ms) INSERT INTO "users" ("accepted_tos", "confirmation_sent_at", "confirmation_token", "confirmed_at", "created_at", "current_sign_in_at", "current_sign_in_ip", "email", "encrypted_password", "faked", "last_sign_in_at", "last_sign_in_ip", "provider", "remember_created_at", "reset_password_sent_at", "reset_password_token", "sign_in_count", "uid", "unconfirmed_email", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20) RETURNING "id" [["accepted_tos", true], ["confirmation_sent_at", nil], ["confirmation_token", nil], ["confirmed_at", Sat, 06 Apr 2013 13:16:42 CEST +02:00], ["created_at", Sat, 06 Apr 2013 13:16:42 CEST +02:00], ["current_sign_in_at", nil], ["current_sign_in_ip", nil], ["email", "app+4ss3t1sal5.1cppf6a.d25089e10cdac6d19ac26ad2c44e14cb@proxymail.facebook.com"], ["encrypted_password", "$2a$10$xjK8Y0zpkdE/PZnT4cSQ5uuJQ/KaPfkhbxFtvYXJSyAAZWV5emy4K"], ["faked", nil], ["last_sign_in_at", nil], ["last_sign_in_ip", ni
app web.1 - - l], ["provider", "facebook"], ["remember_created_at", nil], ["reset_password_sent_at", nil], ["reset_password_token", nil], ["sign_in_count", 0], ["uid", "1503444170"], ["unconfirmed_email", nil], ["updated_at", Sat, 06 Apr 2013 13:16:42 CEST +02:00]]
app web.1 - - (12.1ms) COMMIT
app web.1 - - Completed 500 Internal Server Error in 329ms
app web.1 - -
app web.1 - - TypeError (can't dup NilClass): app/models/user.rb:91:in `find_for_facebook_oauth'
app/controllers/users/omniauth_callbacks_controller.rb:12:in `facebook' EXCEPT
app web.1 - -
app web.1 - -
app web.2 - - Started GET "/" for 2.230.67.191 at 2013-04-06 11:16:45 +0000
エラーに関する手がかりと、それを回避/キャッチする方法は素晴らしいでしょう。ありがとうございました。