CarlosG90 CarlosG90 - 4 months ago 13
Ruby Question

ActionController::ParamaterMissing while running test. From Chapter 7 of Hartl's Tutorial

Currently doing Hartl's (updated) Tutorial and I'm stuck at the end of Chapter 7 [7.5.3 to be specific, Production Deployment].

I'm running a test and getting the following error:

1) Error:
UsersSignupTest#test_invalid_signup_information:
ActionController::ParameterMissing: param is missing or the value is empty: user
app/controllers/users_controller.rb:24:in `user_params'
app/controllers/users_controller.rb:12:in `create'
test/integration/users_signup_test.rb:8:in `block (2 levels) in <class:UsersSignupTest>'
test/integration/users_signup_test.rb:7:in `block in <class:UsersSignupTest>'


2) Error:
UsersSignupTest#test_valid_signup_information:
ActionController::ParameterMissing: param is missing or the value is empty: user
app/controllers/users_controller.rb:24:in `user_params'
app/controllers/users_controller.rb:12:in `create'
test/integration/users_signup_test.rb:19:in `block (2 levels) in <class:UsersSignupTest>'
test/integration/users_signup_test.rb:18:in `block in <class:UsersSignupTest>'


users_controller.rb

class UsersController < ApplicationController

def show
@user = User.find(params[:id])
end

def new
@user = User.new
end

def create
@user = User.new(user_params)
if @user.save
flash[:success] = "Welcome to the Sample App!"
redirect_to @user
else
render 'new'
end
end

private

def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation)
end
end


users_signup_test.rb

require 'test_helper'

class UsersSignupTest < ActionDispatch::IntegrationTest

test "invalid signup information" do
get signup_path
assert_no_difference 'User.count' do
post users_path, params: { user: { name: "",
email: "user@invalid",
password: "foo",
password_confirmation: "bar" } }
end
end


test "valid signup information" do
get signup_path
assert_difference 'User.count', 1 do
post users_path, params: { user: { name: "Example User",
email: "user@example.com",
password: "password",
password_confirmation: "password" } }
end
follow_redirect!
assert_template 'users/show'
end
end

Answer

You have params, instead of user.

Change this:

 params: { name:  "", 
                      email: "user@invalid",
                      password: "foo",
                      password_confirmation: "bar" } }
       end
   end 

to this:

  user: { name:  "", 
                          email: "user@invalid",
                          password: "foo",
                          password_confirmation: "bar" } }
          end
       end 
Comments