我使用的投omniauth轨和我得到这个错误(I am using rails cast omnia

2019-08-03 04:44发布

我使用的MongoDB在轨数据库和我用/认证/ LinkedIn /回调时遇到错误

NoMethodError在AuthenticationsController#创建未定义的方法[]”为无:NilClass Rails.root:/家庭/炳/音乐/ heronhrm应用程序跟踪| 框架跟踪| 完整跟踪应用程序/模型/ user.rb:57:在apply_omniauth '应用程序/控制器/ authentications_controller.rb:19:'创建'

此外,当我删除self.email = omniauth['user_info']['email'] if email.blank? 从的usermodel然后验证错误出现在

用户/ sign_up电子邮件地址不能为空我想要实现的叽叽喳喳,linkdin和Facebook。

我authentication.rb

 class Authentication
  include Mongoid::Document

 belongs_to :user
    field :user_id, :type => String
    field :provider, :type => String
    field :uid, :type => String
 def self.find_by_provider_and_uid(provider, uid)
  where(provider: provider, uid: uid).first
end

end

我的用户模型是这样的

def apply_omniauth(omniauth)
    self.email = omniauth['user_info']['email'] if email.blank?
    authentications.build(:provider => omniauth['provider'], :uid => omniauth['uid'])
  end

  def password_required?
    (authentications.empty? || !password.blank?) && super
  end

我的认证控制器是这样的

    class AuthenticationsController < ApplicationController
  def index
    @authentications = current_user.authentications if current_user
  end

 def create
    omniauth = request.env["omniauth.auth"]
    authentication = Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid'])
    if authentication
      flash[:notice] = "Signed in successfully."
      sign_in_and_redirect(:user, authentication.user)
    elsif current_user
      current_user.authentications.create!(:provider => omniauth['provider'], :uid => omniauth['uid'])
      flash[:notice] = "Authentication successful."
      redirect_to authentications_url
    else
      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
    end
  end
  def destroy
    @authentication = current_user.authentications.find(params[:id])
    @authentication.destroy
    flash[:notice] = "Successfully destroyed authentication."
    redirect_to authentications_url
  end

protected

  # This is necessary since Rails 3.0.4
  # See https://github.com/intridea/omniauth/issues/185
  # and http://www.arailsdemo.com/posts/44
  def handle_unverified_request
    true
    end
    end

我的注册控制器以这样的

class RegistrationsController < Devise::RegistrationsController
  def create
    super
    session[:omniauth] = nil unless @user.new_record?
  end

  private

  def build_resource(*args)
    super
    if session[:omniauth]
      @user.apply_omniauth(session[:omniauth])
      @user.valid?
    end
  end
end

Answer 1:

里面你的应用程序/模型/ authentication.rb添加此

def self.find_by_provider_and_uid(provider, uid)
  where(provider: provider, uid: uid).first
end


Answer 2:

你在你的模型添加此? 如果没有添加,然后添加这一点,然后尝试

key :provider, String
key :uid, String


文章来源: I am using rails cast omniauth and i get this error