我是Rails的新手,我几乎不知道自己在做什么。但。问题是:使用Devise结果注册新用户:
SQLite3::ConstraintException: column email is not unique:
INSERT INTO "users" ("created_at","encrypted_password", "name", "updated_at")
VALUES (?, ?, ?, ?)
请求参数:
{"utf8"=>"✓",
"authenticity_token"=>"1bgk4ovS3JitphVkIvcCZi3ex8QsBq4eEf6ZihQLiHg=",
"user"=>{"name"=>"Someone",
"email"=>"8@prosto.me",
"password"=>"[FILTERED]"},
"commit"=>"Sign up"}
用户模型:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable;
end
数据库迁移:
class DeviseCreateUsers < ActiveRecord::Migration
def self.up
change_table(:users) do |t|
## Database authenticatable
t.string :email, :null => false, :default => ""
t.string :name, :null => false, :default => ""
t.string :encrypted_password, :null => false, :default => ""
## Recoverable
t.string :reset_password_token
t.datetime :reset_password_sent_at
## Rememberable
t.datetime :remember_created_at
## Trackable
t.integer :sign_in_count, :default => 0
t.datetime :current_sign_in_at
t.datetime :last_sign_in_at
t.string :current_sign_in_ip
t.string :last_sign_in_ip
## Confirmable
# t.string :confirmation_token
# t.datetime :confirmed_at
# t.datetime :confirmation_sent_at
# t.string :unconfirmed_email # Only if using reconfirmable
## Lockable
# t.integer :failed_attempts, :default => 0 # Only if lock strategy is :failed_attempts
# t.string :unlock_token # Only if unlock strategy is :email or :both
# t.datetime :locked_at
## Token authenticatable
# t.string :authentication_token
# Uncomment below if timestamps were not included in your original model.
# t.timestamps
end
add_index :users, :email, :unique => true
add_index :users, :name, :unique => true
add_index :users, :reset_password_token, :unique => true
# add_index :users, :confirmation_token, :unique => true
# add_index :users, :unlock_token, :unique => true
# add_index :users, :authentication_token, :unique => true
end
def self.down
# By default, we don't want to make any assumption about how to roll back a migration when your
# model already existed. Please edit below which fields you would like to remove in this migration.
end
end
请告诉我是否需要提供任何其他代码。 并提前感谢你的帮助。
使用数据库架构更新:
ActiveRecord::Schema.define(version: 20131012114812) do
create_table "users", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.string "name"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
更新2 :身份验证也存在问题。对于任何以前成功注册的用户,Devise都会告诉“无效的电子邮件或密码”以尝试登录。
答案 0 :(得分:4)
SQLite告诉你它正在尝试创建一条新记录,其中一个值将是一封电子邮件,但已经存在该电子邮件的现有记录。
读取数据库记录的简便方法是在终端窗口中查询数据库:
$ rails console
$ User.all
如果你想看到你的测试数据库,它将加载你的灯具:
$ rails console -e=test
$ User.all
查找与您尝试创建的电子邮件具有相同电子邮件的记录。
如果这是你第一次使用Devise,你应该检查你的灯具。 Devise当前默认为两个没有属性的灯具。如果您在测试环境中运行,那么这些灯具将作为两个记录加载到测试数据库中,其中包含电子邮件的nil值,这是重复的电子邮件值。像下面那样的灯具会让你传递错误信息。
file: app/test/fixtures/users.yml
one:
email: user@example.com
encrypted_password: password1
two:
email: user2@example.com
encrypted_password: password2
答案 1 :(得分:1)
尝试向User
模型添加唯一性验证:
validates_uniqueness_of :email, :allow_blank => true
这将重新呈现您的用户创建表单,而不是导致错误。
答案 2 :(得分:1)
该数据库中是否有其他“电子邮件”列?
也许您已经有了一个“用户”表,其中已经使用Devise复制了电子邮件列。如果你能告诉我们你的表有哪些列,那将会很有帮助:)