TheNerdyMusician
TheNerdyMusician

Reputation: 13

Hartl's Ruby on Rails Section 7.3 Tutorial Sign-up Test Failure

After coding users_signup_test.rb and expecting a GREEN response, it is still failing for some reason. The tutorial states I should be getting a GREEN response at this phase, but I am not. This is what I receive:

1) Error:
UsersSignupTest#test_invalid_signup_information:
ActiveModel::ForbiddenAttributesError: ActiveModel::ForbiddenAttributesError
    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>'

16 runs, 31 assertions, 0 failures, 1 errors, 0 skips

The only deviation I've had to do from the book that may affect this is one I had to make in the user.rb model and it is as follows:

validates( :email, presence: true, length: { maximum: 255}, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false } )

as opposed to the book's:

validates( :email, presence: true, length: { maximum: 255}, format: { with: VALID_EMAIL_REGEX }, uniqueness: case_sensitive: false )

which would give me an error.

Here is my users_signup_test.rb file:

require 'test_helper'

class UsersSignupTest < ActionDispatch::IntegrationTest

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

end

Let me know if any other code is needed to help diagnose this issue.

As an additional note, I also ran into issues that others have as well using sass-rails 5.0.1 and so I am using sass-rails 5.0.2.

Thank you everyone for your help.

Upvotes: 0

Views: 248

Answers (2)

TheNerdyMusician
TheNerdyMusician

Reputation: 13

Found the answer!

Sorry for the unnecessary post. Researched the error and found it's related to Strong Params. Looking closely at the book, I missed this line:

@user = User.new(user_params)

which changed from

@user = User.new(params[:user])

So I wasn't passing the parameters to the controller properly. Sorry everyone!

Upvotes: 1

soutoner
soutoner

Reputation: 559

Do you have well-defined the needed parameters (user_params function) when you create an user ? If you're following the tutorial you should end with something like this in your users_controller.rb:

class UsersController < ApplicationController

  def create
    @user = User.new(user_params)
    # more logic
  end

  private

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

Because it looks like you're passing an user parameter that is not permitted by the controller.

Upvotes: 2

Related Questions