I'm filling out the sign up form on my site. I type in first name, last name, email, password, and password confirmation. When I click "submit", my form clears, and the console tells me the user already exists (even though it doesnt, and throws me a success message). In short: I'm trying to create a new user, and save it to my database.
See below:
User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE "users"."firstname" IS NULL LIMIT 1 (0.1ms) rollback transaction
Rendered users/new.html.erb within layouts/application (2.4ms) Completed 200 OK in 136ms (Views: 68.4ms | ActiveRecord: 0.9ms)
See code below - cheers!
users_controller.rb
class UsersController < ApplicationController
def create
@user = User.new(user_params)
if @user.save
flash[:success] = "You signed up successfully"
flash[:color] = "valid"
redirect_to @user
else
flash[:notice] = "Form is invalid"
flash[:color] = "invalid"
render "new"
end
end
private
def user_params
params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation)
end
def show
@user = User.find(params[:id])
end
def new
end
end
user.rb
class User < ActiveRecord::Base
validates_length_of :password, :in => 6..20, :on => :create
validates :password_confirmation, presence: true, if: -> { new_record? || changes["password"] }
end
new.html.erb
<%= form_for(:user, :url => {:controller => 'users', :action => 'create'}) do |f| %>
<%= render 'shared/error_messages' %>
</br> <%= f.text_field :firstname, placeholder: 'First Name' %>
</br> <%= f.text_field :lastname, placeholder: 'Last Name' %>
</br> <%= f.text_field :email, placeholder: 'Email' %>
</br> <%= f.password_field :password, placeholder: 'Password' %>
</br> <%= f.password_field :password_confirmation, placeholder: 'Confirm Password' %>
<%= f.submit :Register %>
<% end %>