【问题标题】:I am using rails cast omniauth and i get this error我正在使用 rails castomniauth,我收到此错误
【发布时间】:2012-11-26 08:31:27
【问题描述】:

我在 Rails 中使用 Mongodb 作为数据库,使用 /auth/linkedin/callback 时出错

NoMethodError in AuthenticationsController#create undefined method []' for nil:NilClass Rails.root: /home/prem/Music/heronhrm Application Trace |框架跟踪 |完整跟踪 app/models/user.rb:57:in apply_omniauth' app/controllers/authentications_controller.rb:19:in `create'

此外,当我从 usermodel 中删除 self.email = omniauth['user_info']['email'] if email.blank? 时,会出现验证错误

users/sign_up 电子邮件不能为空 我想为 twitter、linkdin 和 facebook 实现。

我的身份验证.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

【问题讨论】:

  • 好吧,您的omniauth 为nil 或omniauth['user_info'] 为nil。在 apply_omniauth 函数的开头添加 puts omniauth.inspect 以查看问题所在。

标签: ruby-on-rails ruby-on-rails-3 devise mongoid omniauth


【解决方案1】:

在你的 app/models/authentication.rb 添加这个

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

【讨论】:

  • 感谢它解决了我之前的问题,但现在我在 AuthenticationsController#create undefined method []' for nil:NilClass Rails.root: /home/prem/Music/heronhrm Application Trace | Framework Trace | Full Trace app/models/user.rb:57:in apply_omniauth' app/controllers/authentications_controller.rb:19:in `create' 中获取此错误 NoMethodError
  • 是的,但发生了同样的错误。我已经在我的身份验证控制器中添加了这个
  • 你不应该把它放在 authentications_controller.rb 中,而应该放在 application_controller.rb 中
  • 您能否向我们提供您的注册控制器?
  • gist.github.com/2634b6ed5cbfa3bec165 这是我的 apply_omniauth 方法。我看不出你有什么问题,希望这会有所帮助。
【解决方案2】:

您是否在模型中添加了这个?如果未添加则添加此然后尝试

key :provider, String
key :uid, String

【讨论】:

  • 哪个型号?用户或身份验证?
  • 你在用户模型中写了什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-16
  • 2013-07-31
  • 2018-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多