OmniAuth FaceBook登录我的webapp时遇到了一些问题。 请告诉我,我应该编辑什么来修复此错误?检查代码。
浏览器出错:
未定义的方法`to_a' for" Name Surname":String
User.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
devise.rb:
config.omniauth :facebook, '*********', '*********', {:client_options => {:ssl => {:verify => false}}}
答案 0 :(得分:0)
name是一个字符串,您正在尝试将其转换为数组
x = auth.info.name.to_a
使用split
代替
x = auth.info.name.split
#=> ['Name', 'Surname']
答案 1 :(得分:0)
您可以使用OmniAuth first_name和last_name:
更新你的devise.rb:
config.omniauth :facebook, '*********', '*********', info_fields: 'email, first_name, last_name', {:client_options => {:ssl => {:verify => false}}}
和你的user.rb:
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.name = auth.info.first_name
user.surname = auth.info.last_name
user.login = auth.info.uid
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
end
end
答案 2 :(得分:0)
你的user.rb应该是这样的
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
x = auth.info.name.split
if x.count > 1
user.name = x[0]
user.surname = x[1]
else
user.name = x[0]
end
user.login = auth.info.uid
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
end
end
但我建议将其存储在一个字段中,因为名称可能超过两个字,因此很难识别名字和姓氏。