【发布时间】:2016-07-08 12:05:48
【问题描述】:
我在 OmniAuth FaceBook 登录到我的 web 应用时遇到了一些问题。 请告诉我,我应该编辑什么来修复这个错误?检查代码。
浏览器出错:
“姓氏”的未定义方法`to_a':字符串
用户.rb:
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
x = auth.info.name.to_a
user.name = x[0]
user.surname = x[1]
user.login = auth.info.uid
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
end
end
Omniauth_callback_controller.rb:
def facebook
if request.env["omniauth.auth"].info.email.blank?
redirect_to "/users/auth/facebook?auth_type=rerequest&scope=email"
end
@user = User.from_omniauth(request.env["omniauth.auth"])
if @user.persisted?
sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
else
session["devise.facebook_data"] = request.env["omniauth.auth"]
if(@user.surname.nil?)
redirect_to new_profile_path
else
redirect_to my_profile_path
end
end
end
设计.rb:
config.omniauth :facebook, '*********', '*********', {:client_options => {:ssl => {:verify => false}}}
【问题讨论】:
-
Facebook auth 可以分别提供
auth.info.first_name和auth.info.last_name,如果用户有两个以上的名字通过这种方式访问会更可靠。请参阅其他 SO question。
标签: ruby-on-rails facebook devise omniauth