0

我对 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)已填写。我在用户模型中添加了姓名和电子邮件。也许这里有问题?这可能有帮助吗?

4

2 回答 2

1

每当我输入p auth.info.email(对于您需要添加的许多东西.info)时,服务器日志都会显示电子邮件地址。

我认为这与我更改了用户模型有关:

  def self.from_omniauth(auth)
    where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user|
      user.provider = auth.provider
      user.uid = auth.uid
      user.email = auth.info.email
      user.name = auth.info.name
      user.save!
    end
  end

现在错误消失了。我添加了user.save!我更改为auth.info.email并替换first_or_createfirst_or_initialize.tap

因此,当我查看控制台 ( User.all) 时,电子邮件、姓名、uid 和提供者都在数据库中。

显然现在一切正常。坦克伤害你的帮助!

ps:服务器日志还是打了问号,但确实把数据保存在了记录中。

于 2013-02-28T18:44:57.970 回答
0

我猜你要么尝试创建一个没有所有必填字段的用户,要么你试图创建一个已经存在的用户(我怀疑电子邮件存在唯一性约束)。

消除后者;从 users 表中删除所有用户。

测试前者;尝试使用与您提供给该方法User.create(some_params)的参数相同的参数创建用户。from_omniauth要弄清楚这些,请使用 pry 之类的方法简单地执行以下操作:

def self.from_omniauth(auth)
    p auth # Add this line and watch the logs.
    ....
end

也可能有帮助的是查看您的db/schema.rb(或db/structure.sql运行后rake db:structure:dump)并查看是否有任何 NOT NULL 或 UNIQUE 约束。

于 2013-02-26T15:49:08.567 回答