Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby User Authentication with Rails Password Hashing and Sign In Using has_secure_password

Christopher Quinn
Christopher Quinn
9,943 Points

expected user count error

I'm sure there is something wrong in my code, but I cannot be sure. I've poured over it several times and I'm just missing something. Getting this error:

Failures:

  1) Signing up allows a user to sign up for the site and creates the object in the database
     Failure/Error: expect(User.count).to eq(1)

       expected: 1
            got: 0

       (compared using ==)
     # ./spec/features/users/registration_spec.rb:18:in `block (2 levels) in <top (required)>'

registration_spec.rb

require "spec_helper"

describe "Signing up" do
    it "allows a user to sign up for the site and creates the object in the database" do
        expect(User.count).to eq(0)

        visit "/"
        expect(page).to have_content("Sign Up")
        click_link "Sign Up"

        fill_in "First Name", with: "Jason"
        fill_in "Last Name", with: "Seifer"
        fill_in "Email", with: "jason@teamtreehouse.com"
        fill_in "Password", with:" treehouse1234"
        fill_in "Password (again)", with: "treehouse1234"
        click_button "Sign Up"

        expect(User.count).to eq(1)
    end
end

_form.html.erb

<%= form_for(@user) do |f| %>
  <% if @user.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>

      <ul>
      <% @user.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :first_name, "First Name" %><br>
    <%= f.text_field :first_name %>
  </div>
  <div class="field">
    <%= f.label :last_name, "Last Name" %><br>
    <%= f.text_field :last_name %>
  </div>
  <div class="field">
    <%= f.label :email %><br>
    <%= f.text_field :email %>
  </div>
  <div class="field">
    <%= f.label :password %><br>
    <%= f.password_field :password %>
  </div>
  <div class="field">
    <%= f.label :password_confirmation, "Password (again)" %><br>
    <%= f.password_field :password_confirmation %>
  </div>
  <div class="actions">
    <%= f.submit "Sign Up" %>
  </div>
<% end %>

in the above section the first two and last two lines are being removed by the code block and I do not know why. I assure you they're there.

users_controller.rb

class UsersController < ApplicationController
  before_action :set_user, only: [:show, :edit, :update, :destroy]

  # GET /users/new
  def new
    @user = User.new
  end

  # GET /users/1/edit
  def edit
  end

  # POST /users
  # POST /users.json
  def create
    @user = User.new(user_params)

    respond_to do |format|
      if @user.save
        format.html { redirect_to @user, notice: 'User was successfully created.' }
        format.json { render :show, status: :created, location: @user }
      else
        format.html { render :new }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /users/1
  # PATCH/PUT /users/1.json
  def update
    respond_to do |format|
      if @user.update(user_params)
        format.html { redirect_to @user, notice: 'User was successfully updated.' }
        format.json { render :show, status: :ok, location: @user }
      else
        format.html { render :edit }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /users/1
  # DELETE /users/1.json
  def destroy
    @user.destroy
    respond_to do |format|
      format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_user
      @user = User.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def user_params
      params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation)
    end
end

If you require any additional information I will provide it.

1 Answer

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

Does the user get created properly when you sign up manually through the browser, mirroring the steps in capybara spec?

Christopher Quinn
Christopher Quinn
9,943 Points

Yes it does get created properly. Part of me thinks I should just let this error slide since the functionality is there.

Did anyone ever find a solution to this?