SQLite3 :: ConstraintException:UNIQUE约束失败:users.email:INSERT INTO“users”(“name”,“email”,“created_at”,“updated_at”,“password_digest”)VALUES(?,?,?, ?,?)
def create
@user = User.new(user_params)
if @user.save #red line here
else
render 'new'
users_controller.rb
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
end
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
# Handle a successful save.
else
render 'new'
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation)
end
end
用户模型 user.rb
class User < ApplicationRecord
before_save { self.email = email.downcase }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX }
has_secure_password
validates :password, presence: true, length: { minimum: 6 }
end
enter code here
答案 0 :(得分:1)
错误表示您对数据库中的字段有约束,在本例中为表users
,字段email
,并且您正在插入已存在的电子邮件。转到查看用户表的迁移。