私はレールアプリを開発しています。omniauthを使ってfacebook / twitter / linkedinからログインしてください。これまでのところ、ユーザーは認証を使用してサインアップしてアカウントを作成することができますが、検証に合格する必要があるため、有効な名前、ユーザー名、電子メールを入力する必要があるサインアップページに転送されます。これらのフィールドは、request.env ["omniauth.auth"]ハッシュを使用して、可能であれば既に入力しておきたいです。
ここに私のコードです:
authentications_controller.rb
user = User.new
user.apply_omniauth(omniauth)
if user.save
flash[:notice] = "Signed in successfully."
sign_in_and_redirect(:user, user)
else
session[:omniauth] = omniauth.except("extra")
redirect_to new_user_registration_url
end
registrations_controller.rb:
def build_resource(*args)
super
if session[:omniauth]
@user.apply_omniauth(session[:omniauth])
@user.valid?
end
end
user.rb:
def apply_omniauth(omniauth)
self.name = omniauth["user_info"]["name"] if name.blank?
self.email = omniauth["user_info"]["email"] if email.blank?
authentications.build(:provider => omniauth["provider"], :uid => omniauth["uid"])
end
この線:
self.email = omniauth["user_info"]["email"] if email.blank?
このNoMethodErrorの結果:
undefined method `[]" for nil:NilClass
セッション[:omniauth]は、apply_omniauthメソッドの登録コントローラからomniauthに渡されています。このセッションで名前とメールにアクセスするにはどうすればよいですか?
ありがとう
回答:
回答№1は5素早い回答:
omniauth.info.email # which is the same as omniauth["info"]["email"]
解説:
あなたのコールバックコントローラの最初の行にこれを入れます:
render :text => "<pre>" + env["omniauth.auth"].to_yaml and return
ログインしようとすると、返されたネストされたハッシュのハッシュをよく見ることができます。