【问题标题】:NoMethodError Omniauth Facebook in Rails projectRails 项目中的 NoMethodError Omniauth Facebook
【发布时间】: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_nameauth.info.last_name,如果用户有两个以上的名字通过这种方式访问​​会更可靠。请参阅其他 SO question

标签: ruby-on-rails facebook devise omniauth


【解决方案1】:

name 是一个字符串,您正在尝试将其转换为数组

x = auth.info.name.to_a

改用split

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

【讨论】:

    【解决方案2】:

    您可以使用 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
    

    【讨论】:

    • 当我粘贴 'info_fields: 'email, first_name, last_name' 时,RubyMine 对我大喊语法错误并在这部分代码下划线
    • 根据omniauth文档是正确的:config.omniauth :facebook, "APP_ID", "APP_SECRET", scope: 'email', info_fields: 'email,name' ,如here所示。你试过运行它吗?
    • 现在,当我使用 facebook 登录时...它会将我重定向回我的本地主机,但现在 url 看起来像这样:localhost:3000/#_=_
    • 所以它会带你回到一个非授权保护的页面?你能分享你的用户控制器吗? #__-_ 字符由 Facebook 添加到回调 URL。您是否在您的 routes.rb 中正确设置了 omni_ath 回调?即:devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
    • 是的,我在 routes.rb 中有omniauth
    【解决方案3】:

    你的 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
    

    但我建议存储在单个字段中,因为名称可以包含两个以上的单词,因此很难识别姓名和姓氏。

    this link should also help

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-28
      • 1970-01-01
      • 2016-06-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多