【发布时间】:2014-03-03 04:25:26
【问题描述】:
我正在开发一个允许用户使用omniauth 通过facebook/twitter/linkedin 登录的rails 应用程序。到目前为止,用户可以使用身份验证注册和创建帐户,但他们必须通过验证,因此被转发到注册页面,他们必须在其中输入有效的姓名、用户名和电子邮件。如果可能的话,我希望使用 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。如何访问此会话中的姓名和电子邮件?
谢谢
【问题讨论】:
-
当他们发布 omniauth 1.0 时,他们将 auth['user_info'] 更改为仅 auth['info'] - 一个方便的信息,用于了解您是否正在升级 gem 版本...跨度>
标签: ruby-on-rails session hash omniauth