Agile Web Development with Rails, Edition 4

14.2 Iteration I2: Authenticating Users 13.3 Playtime

14.1 Iteration I1: Adding Users

RuntimeError: Edit test/controllers/users_controller_test.rb failed at makedepot.rb:2834

Traceback:
  

Scaffold the user model

rails generate scaffold User name:string password:digest
      invoke  active_record
      create    db/migrate/20160127205644_create_users.rb
      create    app/models/user.rb
      invoke    test_unit
      create      test/models/user_test.rb
      create      test/fixtures/users.yml
      invoke  resource_route
       route    resources :users
      invoke  scaffold_controller
      create    app/controllers/users_controller.rb
      invoke    erb
      create      app/views/users
      create      app/views/users/index.html.erb
      create      app/views/users/edit.html.erb
      create      app/views/users/show.html.erb
      create      app/views/users/new.html.erb
      create      app/views/users/_form.html.erb
      invoke    test_unit
      create      test/controllers/users_controller_test.rb
      invoke    helper
      create      app/helpers/users_helper.rb
      invoke      test_unit
      invoke    jbuilder
      create      app/views/users/index.json.jbuilder
      create      app/views/users/show.json.jbuilder
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/users.coffee
      invoke    scss
      create      app/assets/stylesheets/users.scss
      invoke  scss
   identical    app/assets/stylesheets/scaffolds.scss

uncomment out bcrypt

edit Gemfile
# Use ActiveModel has_secure_password
gem 'bcrypt', '~> 3.1.7'

Restart the server.

Run the migration

rails db:migrate
== 20160127205644 CreateUsers: migrating ======================================
-- create_table(:users)
   -> 0.0025s
== 20160127205644 CreateUsers: migrated (0.0025s) =============================
 

Add validation, has_secure_password

edit app/models/user.rb
class User < ApplicationRecord
  validates :name, presence: true, uniqueness: true
  has_secure_password
end

Avoid redirect after create, update operations

edit app/controllers/users_controller.rb
  def create
    @user = User.new(user_params)
 
    respond_to do |format|
      if @user.save
        format.html { redirect_to users_url,
          notice: "User #{@user.name} 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
edit app/controllers/users_controller.rb
  def update
    respond_to do |format|
      if @user.update(user_params)
        format.html { redirect_to users_url,
          notice: "User #{@user.name} 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

Display users sorted by name

edit app/controllers/users_controller.rb
  def index
    @users = User.order(:name)
  end

Add Notice

edit app/views/users/index.html.erb
<p id="notice"><%= notice %></p>
 
<h1>Users</h1>
<% if notice %>
<p id="notice"><%= notice %></p>
<% end %>
 
<table>
  <thead>
    <tr>
      <th>Name</th>
      <th colspan="3"></th>
    </tr>
  </thead>
 
  <tbody>
    <% @users.each do |user| %>
      <tr>
        <td><%= user.name %></td>
        <td><%= link_to 'Show', user %></td>
        <td><%= link_to 'Edit', edit_user_path(user) %></td>
        <td><%= link_to 'Destroy', user, method: :delete,
        data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>
 
<br>
 
<%= link_to 'New User', new_user_path %>

Update form used to both create and update users

edit app/views/users/_form.html.erb
<div class="depot_form">
 
<%= 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 |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
 
  <fieldset>
  <legend>Enter User Details</legend>
 
  <div class="field">
    <%= f.label :name, 'Name:' %>
    <%= f.text_field :name, size: 40 %>
  </div>
 
  <div class="field">
    <%= f.label :password, 'Password:' %>
    <%= f.password_field :password, size: 40 %>
  </div>
 
  <div class="field">
    <%= f.label :password_confirmation, 'Confirm:' %>
    <%= f.password_field :password_confirmation, size: 40 %>
  </div>
 
  <div class="actions">
    <%= f.submit %>
  </div>
 
  </fieldset>
<% end %>
 
</div>

Demonstrate creating a new user

get /users

Users

Name

New User
get /users/new

New User

Enter User Details
Back
post /users
You are being redirected.
get http://localhost:3000/users

User dave was successfully created.

Users

User dave was successfully created.

Name
dave Show Edit Destroy

New User

Show how this is stored in the database

rails db:select * from users
rails aborted!
Don't know how to build task 'db:select' (see --tasks)
/home/rubys/git/rails/railties/lib/rails/commands/rake_proxy.rb:13:in `block in run_rake_task'
/home/rubys/git/rails/railties/lib/rails/commands/rake_proxy.rb:10:in `run_rake_task'
/home/rubys/git/rails/railties/lib/rails/commands/commands_tasks.rb:51:in `run_command!'
/home/rubys/git/rails/railties/lib/rails/command.rb:20:in `run'
/home/rubys/git/rails/railties/lib/rails/commands.rb:19:in `<top (required)>'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
(See full trace by running task with --trace)

Update tests to reflect the changes in redirection and uniqueness

edit test/controllers/users_controller_test.rb
#<IndexError: regexp not matched>
  /home/rubys/git/gorp/lib/gorp/edit.rb:91:in `edit'
  makedepot.rb:2834:in `block (3 levels) in <main>'
  /home/rubys/git/gorp/lib/gorp/edit.rb:111:in `instance_exec'
  /home/rubys/git/gorp/lib/gorp/edit.rb:111:in `block in dcl'
  /home/rubys/git/gorp/lib/gorp/edit.rb:109:in `sub!'
  /home/rubys/git/gorp/lib/gorp/edit.rb:109:in `dcl'
  makedepot.rb:2833:in `block (2 levels) in <main>'
  /home/rubys/git/gorp/lib/gorp/edit.rb:173:in `instance_exec'
  /home/rubys/git/gorp/lib/gorp/edit.rb:173:in `edit'
  makedepot.rb:2832:in `block in <main>'
  /home/rubys/git/gorp/lib/gorp/output.rb:59:in `block (4 levels) in <top (required)>'
  /home/rubys/git/gorp/lib/gorp/output.rb:49:in `each'
  /home/rubys/git/gorp/lib/gorp/output.rb:49:in `block (3 levels) in <top (required)>'
  /home/rubys/.rvm/gems/ruby-2.3.0/gems/builder-3.2.2/lib/builder/xmlbase.rb:175:in `_nested_structures'
  /home/rubys/.rvm/gems/ruby-2.3.0/gems/builder-3.2.2/lib/builder/xmlbase.rb:68:in `tag!'
  /home/rubys/.rvm/gems/ruby-2.3.0/gems/builder-3.2.2/lib/builder/xmlbase.rb:93:in `method_missing'
  /home/rubys/git/gorp/lib/gorp/output.rb:22:in `block (2 levels) in <top (required)>'
  /home/rubys/.rvm/gems/ruby-2.3.0/gems/builder-3.2.2/lib/builder/xmlbase.rb:175:in `_nested_structures'
  /home/rubys/.rvm/gems/ruby-2.3.0/gems/builder-3.2.2/lib/builder/xmlbase.rb:68:in `tag!'
  /home/rubys/.rvm/gems/ruby-2.3.0/gems/builder-3.2.2/lib/builder/xmlbase.rb:93:in `method_missing'
  /home/rubys/git/gorp/lib/gorp/output.rb:11:in `block in <top (required)>'
    
require 'test_helper'
 
class UsersControllerTest < ActionDispatch::IntegrationTest
  setup do
    @user = users(:one)
  end
 
  test "should get index" do
    get users_url
    assert_response :success
  end
 
  test "should get new" do
    get new_user_url
    assert_response :success
  end
 
  test "should create user" do
    assert_difference('User.count') do
      post users_url, params: { user: { name: @user.name, password: 'secret', password_confirmation: 'secret' } }
    end
 
    assert_redirected_to user_path(User.last)
  end
 
  test "should show user" do
    get user_url(@user)
    assert_response :success
  end
 
  test "should get edit" do
    get edit_user_url(@user)
    assert_response :success
  end
 
  test "should update user" do
    patch user_url(@user), params: { user: { name: @user.name, password: 'secret', password_confirmation: 'secret' } }
    assert_redirected_to user_path(@user)
  end
 
  test "should destroy user" do
    assert_difference('User.count', -1) do
      delete user_url(@user)
    end
 
    assert_redirected_to users_path
  end
end
edit test/controllers/users_controller_test.rb

Make sure that all test names are unique

edit test/fixtures/users.yml
# Read about fixtures at
# http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
 
one:
  name: dave
  password_digest: <%= BCrypt::Password.create('secret') %>
 
two:
  name: susannah
  password_digest: <%= BCrypt::Password.create('secret') %>
rails test
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from included at /home/rubys/.rvm/gems/ruby-2.3.0/gems/turbolinks-2.5.3/lib/turbolinks/xhr_url_for.rb:7)
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from included at /home/rubys/.rvm/gems/ruby-2.3.0/gems/turbolinks-2.5.3/lib/turbolinks/xhr_url_for.rb:7)
rails aborted!
ActiveRecord::NoEnvironmentInSchemaError: 

    
Environment data not found in the schema. To resolve this issue, run: 

    
	bin/rails db:environment:set RAILS_ENV=test

    
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:1259:in `last_stored_environment'
/home/rubys/git/rails/activerecord/lib/active_record/tasks/database_tasks.rb:48:in `check_protected_environments!'
/home/rubys/git/rails/activerecord/lib/active_record/railties/databases.rake:11:in `block (2 levels) in <top (required)>'
/home/rubys/git/rails/activerecord/lib/active_record/railties/databases.rake:375:in `block (3 levels) in <top (required)>'
/home/rubys/git/rails/railties/lib/rails/commands/rake_proxy.rb:13:in `block in run_rake_task'
/home/rubys/git/rails/railties/lib/rails/commands/rake_proxy.rb:10:in `run_rake_task'
/home/rubys/git/rails/railties/lib/rails/commands/commands_tasks.rb:51:in `run_command!'
/home/rubys/git/rails/railties/lib/rails/command.rb:20:in `run'
/home/rubys/git/rails/railties/lib/rails/commands.rb:19:in `<top (required)>'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Tasks: TOP => db:test:load => db:test:purge => db:check_protected_environments
(See full trace by running task with --trace)
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:578:in `check_pending!':  (ActiveRecord::PendingMigrationError)

    
Migrations are pending. To resolve this issue, run:

    
	bin/rails db:migrate RAILS_ENV=test

    
	from /home/rubys/git/rails/activerecord/lib/active_record/migration.rb:591:in `load_schema_if_pending!'
	from /home/rubys/git/rails/activerecord/lib/active_record/migration.rb:597:in `block in maintain_test_schema!'
	from /home/rubys/git/rails/activerecord/lib/active_record/migration.rb:828:in `suppress_messages'
	from /home/rubys/git/rails/activerecord/lib/active_record/migration.rb:602:in `method_missing'
	from /home/rubys/git/rails/activerecord/lib/active_record/migration.rb:597:in `maintain_test_schema!'
	from /home/rubys/git/rails/railties/lib/rails/test_help.rb:15:in `<top (required)>'
	from /home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:302:in `require'
	from /home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:302:in `block in require'
	from /home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:268:in `load_dependency'
	from /home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:302:in `require'
	from /home/rubys/git/awdwr/edition4/work-230/depot/test/test_helper.rb:3:in `<top (required)>'
	from /home/rubys/git/awdwr/edition4/work-230/depot/test/controllers/carts_controller_test.rb:1:in `require'
	from /home/rubys/git/awdwr/edition4/work-230/depot/test/controllers/carts_controller_test.rb:1:in `<top (required)>'
	from /home/rubys/git/rails/railties/lib/rails/test_unit/test_requirer.rb:11:in `require'
	from /home/rubys/git/rails/railties/lib/rails/test_unit/test_requirer.rb:11:in `block in require_files'
	from /home/rubys/git/rails/railties/lib/rails/test_unit/test_requirer.rb:10:in `each'
	from /home/rubys/git/rails/railties/lib/rails/test_unit/test_requirer.rb:10:in `require_files'
	from /home/rubys/git/rails/railties/lib/rails/test_unit/minitest_plugin.rb:75:in `plugin_rails_init'
	from /home/rubys/.rvm/gems/ruby-2.3.0@global/gems/minitest-5.8.3/lib/minitest.rb:74:in `block in init_plugins'
	from /home/rubys/.rvm/gems/ruby-2.3.0@global/gems/minitest-5.8.3/lib/minitest.rb:72:in `each'
	from /home/rubys/.rvm/gems/ruby-2.3.0@global/gems/minitest-5.8.3/lib/minitest.rb:72:in `init_plugins'
	from /home/rubys/.rvm/gems/ruby-2.3.0@global/gems/minitest-5.8.3/lib/minitest.rb:123:in `run'
	from /home/rubys/git/rails/railties/lib/rails/commands/test.rb:9:in `<top (required)>'
	from /home/rubys/git/rails/railties/lib/rails/commands/commands_tasks.rb:138:in `require'
	from /home/rubys/git/rails/railties/lib/rails/commands/commands_tasks.rb:138:in `require_command!'
	from /home/rubys/git/rails/railties/lib/rails/commands/commands_tasks.rb:95:in `test'
	from /home/rubys/git/rails/railties/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
	from /home/rubys/git/rails/railties/lib/rails/command.rb:20:in `run'
	from /home/rubys/git/rails/railties/lib/rails/commands.rb:19:in `<top (required)>'
	from bin/rails:4:in `require'
	from bin/rails:4:in `<main>'

14.2 Iteration I2: Authenticating Users 13.3 Playtime