0

我几乎已经让 Omniauth w/twitter 工作了,但是当它在验证 twitter 凭据后尝试将我重定向到我的应用程序时,我收到以下冗长的错误:

AuthenticationsController#create 中的 ActiveRecord::StatementInvalid

SQLite3::ConstraintException: constraint failed: INSERT INTO "users" 
("created_at",  "current_sign_in_at", "current_sign_in_ip", "email", 
"encrypted_password", "last_sign_in_at", "last_sign_in_ip", 
"password_salt", "remember_created_at", "remember_token", 
 "reset_password_token", "sign_in_count", "updated_at") VALUES 
 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

用户.rb:

class User < ActiveRecord::Base
  has_many :authentications

  # Include default devise modules. Others available are:
  # :token_authenticatable, :lockable, :timeoutable and :activatable
  devise :database_authenticatable, :registerable, 
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation
  # attr_accessible :title, :body

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

  end

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

  def email_required?
    super && :provider.blank?
  end
end

身份验证控制器:

    class AuthenticationsController < ApplicationController
  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 p user 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

end
4

1 回答 1

0

您可以将 user.save 部分更改为:

p user
if user.save

这会将用户对象的内容记录到您的控制台或日志文件中(取决于您运行 Rails 的方式)。然后,您可以检查输出(类似于#<User id: 15, sign_in_count: 3...)以确保 SQLite 错误中列出的所有列都具有合理的值。例如,您可能会发现数据库中需要“电子邮件”,但您的用户的电子邮件为 nil。

于 2013-02-28T22:11:13.860 回答