【问题标题】:ActiveRecord::RecordInvalid in OmniauthCallbacksController#twitter Validation failed: Email can't be blankOmniauthCallbacksController#twitter 中的 ActiveRecord::RecordInvalid 验证失败:电子邮件不能为空
【发布时间】:2016-08-16 03:49:22
【问题描述】:

在尝试在我的 Rails 应用程序中实现 Omniauth-twitter 时,我遇到了上述错误。 错误集中在我的控制器中的一个语句上,我知道这与 twitter 没有提供回调电子邮件有关。我正在使用设计进行身份验证。我还打算安装 Facebook omniauth,但希望首先让 Twitter 工作。 我可以实施什么代码块来跳过对 Twitter 的验证?它会进入我的控制器或用户模型吗?

这是我的代码 -

OmniauthCallbacksController -

class OmniauthCallbacksController < Devise::OmniauthCallbacksController

def twitter

     @details = request.env["omniauth.auth"]

    @provider = @details["provider"]
    @provider_id = @details["uid"]

    @user = User.where(provider: @provider, provider_id: @provider_id).first

    if @user.present?
        #sign them in
    else
        # make a new user
        @user = User.new
        @user.provider = @provider
        @user.provider_id = @provider_id

        # because of has_secure_password - will this work?
        @user.password = "AAAAAA!!"
        @user.password_confirmation = "AAAAAA!!"

        # let's save the key and secret
        @user.key = @details["credentials"]["token"]
        @user.secret = @details["credentials"]["secret"]

        # lets fill in their details
        @user.name = @details["info"]["name"]
        @user.email = @details["info"]["email"]

        @user.save!
    end


        session[:uid] = @user.id 
        flash[:success] = "You've logged in"
        redirect_to root_path

end

def password_required?
    super && provider.blank?
end






end

routes.rb

Rails.application.routes.draw do

  #get "/auth/:provider/callback" => "social_logins#create"


  devise_for :users, :controllers => { omniauth_callbacks:     "omniauth_callbacks", registrations: 'registrations' }  



  resources :users
  resources :events do

    resources :bookings
  end
  # get 'welcome/index'


  authenticated :user do
    root 'events#index', as: "authenticated_root"
  end


    root 'welcome#index'




end

用户.rb

class User < ActiveRecord::Base

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :omniauthable,     omniauth_providers: [:twitter]

         has_many :events
         has_many :bookings
         has_many :authentications




end

【问题讨论】:

  • 你确定@details["info"]["email"]这不是零吗?
  • 没有。它需要不在此验证范围内,以便回调起作用。
  • :validatable 是验证email 存在的东西。因此,您可能会删除 :validatable 和您自己的验证。见这里github.com/plataformatec/devise/blob/master/lib/devise/models/…
  • 我并不热衷于为此删除验证,这就是为什么我宁愿放置一些代码来避免在 twitter 登录时发生这种情况。

标签: ruby-on-rails activerecord twitter devise omniauth


【解决方案1】:

我在 OmniauthCallbackController 中用这行代码找到了答案

  # lets fill in their details
        @user.name = @details["info"]["name"]
        if @provider == "twitter"? @user.save!(:validate => false) : @user.save!
        # the above if statement allows for twitter to skip validation which requires an email
        @user.email = @details["info"]["email"]
        end

        @user.save!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-30
    相关资源
    最近更新 更多