AuthenticationsController #create中的ActiveRecord :: StatementInvalid

时间:2013-02-28 21:39:12

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

我几乎让Omniauth w / 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 
 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

User.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

1 个答案:

答案 0 :(得分:0)

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

p user
if user.save

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