简体   繁体   English

Rails项目中的NoMethodError Omniauth Facebook

[英]NoMethodError Omniauth Facebook in Rails project

I got some problem with OmniAuth FaceBook login into my webapp. OmniAuth FaceBook登录我的webapp时遇到了一些问题。 Tell me please, what should i edit to fix this error? 请告诉我,我应该编辑什么来修复此错误? Check the code. 检查代码。

Error in the browser: 浏览器出错:

undefined method `to_a' for "Name Surname":String 用于“Name Surname”的未定义方法`to_a':String

User.rb: 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 : 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 : devise.rb:

config.omniauth :facebook, '*********', '*********', {:client_options => {:ssl => {:verify => false}}}

name is a string and you are trying to convert it to array name是一个字符串,您尝试将其转换为数组

x = auth.info.name.to_a

use split instead 请改用split

x = auth.info.name.split
#=> ['Name', 'Surname']

You can make use of OmniAuth first_name and last_name: 您可以使用OmniAuth first_name和last_name:

Update your devise.rb: 更新你的devise.rb:

config.omniauth :facebook, '*********', '*********', info_fields: 'email, first_name, last_name', {:client_options => {:ssl => {:verify => false}}}

and your user.rb: 和你的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

your user.rb should be like this 你的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

But I would suggest to store in a single field, because name can be of more than two words so it would become difficult to identify name and surname. 但我建议将其存储在一个字段中,因为名称可能超过两个字,因此很难识别名称和姓氏。

this link should also help 这个链接也应该有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM