我对 Ruby on Rails 很陌生,所以请多多包涵。
我希望用户能够通过 Facebook 登录。我遵循了 railscast #265 Devise and OmniAuth (revised)。在 railscast 中,用户在登录 Facebook 之前会被要求提供电子邮件地址。我希望用户能够通过 Facebook 登录,而无需事先提供电子邮件地址。
我使用的是:Devise 和 Omniauth-facebook gems
我的期望:用户点击链接,通过 Facebook 登录,Facebook 返回电子邮件、姓名、uid,我的应用程序将其保存到数据库并创建用户。
我得到什么:错误消息ActiveRecord::StatementInvalid in OmniauthCallbacksController#facebook
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", "name", "provider", "remember_created_at", "reset_password_sent_at", "reset_password_token", "sign_in_count", "uid", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
这是我的 user.rb 模型:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :omniauthable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
# attr_accessible :title, :body
# Nodig als :validatable uit is
#validates_presence_of :email, :if => :email_required?
#validates_uniqueness_of :email, :allow_blank => true, :if => :email_changed?
#validates_format_of :email, :with => Devise.email_regexp, :allow_blank => true, :if => :email_changed?
#
#validates_presence_of :password, :if => :password_required?
#validates_confirmation_of :password, :if => :password_required?
#validates_length_of :password, :within => Devise.password_length, :allow_blank => true
def self.from_omniauth(auth)
where(auth.slice(:provider, :uid)).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
user.email = auth.email
user.name = auth.info.name
end
end
def self.new_with_session(params, session)
if session["devise.user_attributes"]
new(session["devise.user_attributes"], without_protection: true) do |user|
user.attributes = params
user.valid?
end
else
super
end
end
def email_required?
provider == :facebook
end
def password_required?
super && provider.blank?
end
def update_with_password(params, *options)
if encrypted_password.blank?
update_attributes(params, *options)
else
super
end
end
end
这是我向用户添加 OmniAuth 的迁移(用户已经有电子邮件)
class AddOmniauthToUsers < ActiveRecord::Migration
def change
add_column :users, :provider, :string
add_column :users, :uid, :string
add_column :users, :name, :string
end
end
这是我的 OmniAuth 回调控制器:
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def all
user = User.from_omniauth(request.env["omniauth.auth"])
if user.persisted?
flash.notice = "Loged in!"
sign_in_and_redirect user
else
session["devise.user_attributes"] = user.attributes
redirect_to new_user_registration_url
end
end
alias_method :facebook, :all
end
提前致谢
编辑:堆栈跟踪:
app/models/user.rb:23:in `from_omniauth'
app/controllers/omniauth_callbacks_controller.rb:3:in `all'
编辑:服务器日志奇怪的是。当我打印时,auth
我可以在服务器日志中看到名称、电子邮件、提供商和 uid。但是,这会出现:
INSERT INTO "users" ("created_at", "current_sign_in_at", "current_sign_in_ip", "email", "encrypted_password", "last_sign_in_at", "last_sign_in_ip", "name", "provider", "remember_created_at", "reset_password_sent_at", "reset_password_token", "sign_in_count", "uid", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [["created_at", Tue, 26 Feb 2013 17:20:23 UTC +00:00], ["current_sign_in_at", nil], ["current_sign_in_ip", nil], ["email", nil], ["encrypted_password", ""], ["last_sign_in_at", nil], ["last_sign_in_ip", nil], ["name", nil], ["provider", "facebook"], ["remember_created_at", nil], ["reset_password_sent_at", nil], ["reset_password_token", nil], ["sign_in_count", 0], ["uid", "XXXXX"], ["updated_at", Tue, 26 Feb 2013 17:20:23 UTC +00:00]]
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", "name", "provider", "remember_created_at", "reset_password_sent_at", "reset_password_token", "sign_in_count", "uid", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
例如名称,电子邮件,nil
但提供者和 uid(现在为 XXXXX)已填写。我在用户模型中添加了姓名和电子邮件。也许这里有问题?这可能有帮助吗?