I'm setting up user auth in my Rails app using Postgres and calling User.new() continuously keeps telling me User Exists despite the user definitely NOT being in my database.
app/controllers/api/user_controller.rb
class Api::UserController < ApplicationController
respond_to :json
protect_from_forgery with: :null_session
def create
@user = User.new(
email: params[:email],
password: params[:password],
password_confirmation: params[:password_confirmation],
first_name: params[:first_name],
last_name: params[:last_name],
groupname: params[:groupname],
admin: params[:admin]
)
if @user.save
puts "it worked"
else
puts "Didn't work"
end
end
end
app/models/user.rb
class User < ApplicationRecord
before_save { self.email = email.downcase }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 }, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }
validates :first_name, presence: true, length: { maximum: 51 }
validates :groupname, presence: true, length: { maximum: 51 }
has_secure_password
validates :password, presence: true, length: { minimum: 6 }
end
app/db.schema.rb
create_table "users", force: :cascade do |t|
t.string "email"
t.string "password_digest"
t.string "first_name"
t.string "last_name"
t.string "groupname"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "admin"
t.index ["email"], name: "index_users_on_email", unique: true
end
terminal response
app/controllers/api/user_controller.rb:31:in `create'
Started POST "/api/user" for 127.0.0.1 at 2018-01-30 19:03:55 -0800
Processing by Api::UserController#create as JSON
Parameters: {"email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "first_name"=>"asdfasfd", "last_name"=>"asdfasddfs", "groupname"=>"fdsafdasf", "admin"=>"true", "user"=>{"email"=>"[email protected]", "first_name"=>"asdfasfd", "last_name"=>"asdfasddfs", "groupname"=>"fdsafdasf", "admin"=>"true"}}
Can't verify CSRF token authenticity.
(0.2ms) BEGIN
User Exists (0.4ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER($1) LIMIT $2 [["email", "[email protected]"], ["LIMIT", 1]]
(0.2ms) ROLLBACK
Didn't work
No template found for Api::UserController#create, rendering head :no_content
Completed 204 No Content in 118ms (ActiveRecord: 6.3ms)
puts "Didn't work"top @user.errors.full_messagesto see why...