我正在使用 Rails Castomniauth 并收到此错误

2023-12-04

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

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

另外当我删除时self.email = omniauth['user_info']['email'] if email.blank?从 usermodel 那么验证错误出现在

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

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

def self.find_by_provider_and_uid(provider, uid)
  where(provider: provider, uid: uid).first
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

我正在使用 Rails Castomniauth 并收到此错误 的相关文章

  • 从 ActiveRecord 获取表名

    I used ActiveRecord Base set table name在动态创建的 ActiveRecord 类上设置我的表名称 现在我需要知道稍后如何获得该值 api 文档没有提及如何执行此操作 另外 我无法从 ActiveRec
  • 如何使用Factory Girl生成回形针附件?

    我有一个包含许多图像的模型 Person 其中图像有一个称为数据的回形针附件字段 下面显示了缩写版本 class Person has many images end class Image has attached file data b
  • 如何验证数值的小数位数?

    我在中间进行了验证 如下所示 validates numericality of shoe size message gt Please input a number 但这还不够 因为用户可以输入一些值 例如 42 222222221212
  • 如何跳过 Devise SessionsController 的 before_filter ?

    我有一个before filter in my ApplicationController 也就是说 对于我的项目中的每个控制器 我怎么能够skip before filter对于设计的SessionsController创造行动 这是我的
  • Rails 中的并行方法

    我的 Rails Web 应用程序有数十种方法 从调用 API 到处理查询结果 这些方法具有以下结构 def method one batch query API process data end def method nth batch
  • 如何在rails中使用npm包?

    我正在尝试使用王牌编辑 https github com ajaxorg ace在我的 Ruby on Rails 应用程序中 大部分视图由 React 组件组成 我正在使用反应轨道宝石 https github com reactjs r
  • ruby on Rails,会话过期通知

    我正在使用 ruby 1 9 3 和 Rails 3 2 我的实际会话处理如下所示 会话助手 def sign in user cookies remember token value user remember token expires
  • Rails:渲染不起作用,仍然出现“模板丢失”

    我目前正在学习 Rails 指南 我完成了这些步骤 但仍然遇到错误 我的 Ruby 版本是ruby 2 1 1p76Rails 版本是4 0 4 按照指南的指示 我创建了一个Article Controller class Articles
  • Memcachier 达到缓存限制时 Heroku 请求超时

    我使用 Memcachier Dalli 作为客户端 将 Rails 应用程序部署到 Heroku 我正在使用免费插件 提供 25 MB 缓存 我们开始收到来自heroku的请求超时 经过调试 我们发现手动刷新Memcachier解决了问题
  • 使用 Minitest 测试自定义验证器

    我有多个带有电子邮件验证的模型 因此 我将验证提取到自定义验证器中 我按照以下教程做到了这一点导轨指南 http guides rubyonrails org active record validations html custom va
  • ActiveRecord3死锁重试

    Rails 3 或 ActiveRecord 3 是否有任何插件可以复制旧版本死锁重试 http agilewebdevelopment com plugins deadlock retry插入 或者 该插件仍然适用于 Rails 3 吗
  • 自定义变形在rails3上不起作用?

    我正在使用 Rails 3 0 1 并在initializers inflections rb 中有以下代码 ActiveSupport Inflector inflections do inflect inflect irregular
  • 带有附加参数的redirect_to

    我是一个菜鸟 redirect to users url notice Succeed p p 然后我添加一个message它失败了 redirect to users url notice Succeed message test p p
  • 在 Rails 中禁用连接池以使用 PgBouncer

    我们有一个 Ruby on Rails 4 2 8 项目 可以访问大型 PostgreSQL 数据库 我们将使用 PgBouncer 添加一个新的连接池服务器 由于 PgBouncer 将处理数据库连接池 我们是否需要关闭 Rails 自动
  • Ruby on Rails 3 - 为每个请求重新加载 lib 目录

    我正在为 Rails 3 应用程序创建一个新引擎 正如您所猜测的 该引擎位于我的应用程序的 lib 目录中 但是 我在开发它时遇到了一些问题 事实上 每次更改引擎中的某些内容时 我都需要重新启动服务器 有办法避免这种情况吗 我可以强制rai
  • 如何从rails控制台将数据添加到数据库

    我有一个User model gt gt u User new gt
  • 使用复选框过滤列表

    我有一个电影列表及其评级 在我的页面顶部 我有一个表单 其中提供了一个复选框列表 其中显示了每个可用的评级 G PG 13 等 一旦用户单击复选框并点击提交 我只想显示所选的电影 在我的索引方法中 我有一个名为的实例变量 filtered
  • Rails Capistrano 3 - 如何将 production.log 文件共享到 Capistrano 的共享文件夹中?

    我正在使用 Capistrano 3 与我的 Rails 应用程序一起部署 问题是 每次我将应用程序部署到服务器时 log development log 文件都会重置 我听说可以将 production log 文件放入共享文件夹中并在每
  • 自定义通用 Rails 错误消息

    我们的 Rails 应用程序被设计为链接到多个客户端数据库的单个代码库 根据子域 应用程序确定要连接到哪个数据库 我们使用液体模板为每个客户定制演示文稿 我们无法为每个客户定制通用的 我们很抱歉 出了点问题 消息 谁能推荐一种方法让我们能够
  • 在 ActiveAdmin 或打印解决方案中动态更改分页

    我是 Activeadmin 和 Rails 的新手 我需要一些帮助 我有一个分页模型 我想允许用户更改分页值或完全禁用它 这样它就可以打印 到打印机 所有记录 或过滤后的记录 我知道我可以在 before filter 中使用 per p

随机推荐