0
votes

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 %>
3
Larger trace, or at least information about what you are doing would be useful...Smar
@Smar 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). I'm trying to create a new user, and save it to my database.Brittany

3 Answers

3
votes

As you can see from the generated SQL, firstname is nil on your new user.

That is because this line contains typos:

params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation

should be:

params.require(:user).permit(:firstname, :lastname, :email, :password, :password_confirmation

0
votes

I think you are missing some params. Using devise? Try to create a new user via rails console to see what parameters are needed.

0
votes

Check the strong parameters given as first_name and last_name but in form it is firstname and lastname.