0
votes

SQLite3::ConstraintException: UNIQUE constraint failed: 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 model 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
1
Apparently you are trying to create a new record with an existing email address and the email field has a unique constraint. It means this column must have unique values.steady_daddy
Can you show your user model, error shows that you have violated unique key constraints, please don't use 2 similar emails for creating object.Vaibhav Dhoke

1 Answers

1
votes

The error says that you have a constraint on a field in the database, in this case tables users, field email, and you are inserting an email already present. Go to see the migration for the users table.