我几乎已经让 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