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

Creating the Friendship error

I'm about 8 minutes into the Creating the friendship video for the ruby on rails project, and when I run the test on user_friendships_controller_test, I get:

test: #create with a valid friend_id should redirect to the profile page of the
friend. (UserFriendshipsControllerTest):
NoMethodError: undefined method `user_friendships' for nil:NilClass
C:/Sites/treebook/app/controllers/user_friendships_controller.rb:20:in `create'

This is from the user_friendships_controller

def create
    if params[:friend_id]
        @friend = User.where(profile_name: params[:friend_id]).first
        @user_friendship = current_user.user_friendships.new(friend: @friend)
        @user_friendship.save
        redirect_to profile_path(@friend)
    else
        flash[:error] = "Friend required"
        redirect_to root_path
    end
end

6 Answers

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Hey Thomas, can you paste the failing test case?

There are a few errors, they scroll of the page even.

test: #create with a valid friend_id should redirect to the profile page of the
friend. (UserFriendshipsControllerTest):NoMethodError: undefined method
user_friendships' for nil:NilClass

but I noticed they had that common thread about the user_friendships. So here's the user_friendship_controller:

class UserFriendshipsController < ApplicationController
before_filter :authenticate_user!, only: [:new]

def new
    if params[:friend_id]
        @friend = User.where(profile_name: params[:friend_id]).first
        raise ActiveRecord::RecordNotFound if @friend.nil?
        #logger.info "Trying to find a friend:"
        #logger.info @friend
        @user_friendship = current_user.user_friendships.new(friend: @friend)
    else
        flash[:error] = "Friend required"
    end
    rescue ActiveRecord::RecordNotFound
        render file: 'public/404', status: :not_found
end
def create
    if params[:friend_id]
        @friend = User.where(profile_name: params[:friend_id]).first
        @user_friendship = current_user.user_friendships.new(friend: @friend)
        @user_friendship.save
        redirect_to profile_path(@friend)
    else
        flash[:error] = "Friend required"
        redirect_to root_path
    end
      end
end

and here's the test (pardon the test usernames):

context "#create" do
    context "when not logged in" do
        should "redirect to the login page" do
            get :new
            assert_response :redirect
            assert_redirected_to login_path
        end
    end
    context "when logged in" do
        setup do
            sign_in users(:baggins)
        end
    end

    context "with no friend_id" do
        setup do
            post:create
        end

        should "set the flash error message" do
            assert !flash[:error].empty?
        end

        should "redirect to the site root" do
            assert_redirected_to root_path
        end
    end

    context "with a valid friend_id" do
        setup do
            post :create, friend_id: users(:gandalf)
        end
        should "assign a friend object" do
            assert assigns(:friend)
            assert_equal users(:gandalf), assigns(:friend)
        end
        should "assign a user_friendship object" do
        assert assigns(:user_friendship)
        assert_equal users(:baggins), assigns(:user_friendship).user
        assert_equal users(:gandalf), assigns(:user_friendship).friend
        end

        should "create a friendship" do
            assert users(:baggins).friends.include?(users(:gandalf))
        end

        should "redirect to the profile page of the friend" do
            assert_response :redirect
            assert_redirected_to profile_path(users(:mike))
        end

        should "set the flash sucess message" do
            assert flash[:success]
            assert_equal "You are now friends with #{users(:gandalf)}", flash[:success]
        end
    end
end
end
Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

In the valid friend id test try changing:

            post :create, friend_id: users(:gandalf)

To

            post :create, friend_id: users(:gandalf).profile_name

Thanks! but, it's still giving me this error:

test: #create with a valid friend_id should create a friendship. (UserFriendship
sControllerTest):
NoMethodError: undefined method `user_friendships' for nil:NilClass
C:/Sites/treebook/app/controllers/user_friendships_controller.rb:20:in `create'

Ok, I've rolled it back, and I'm on the creating the friendship form, and I have another inexplicable failure.

test: #new when logged in should ask if you really want to friend the user.
(UserFriendshipsControllerTest) [test/functional/user_friendships_controller_test.rb:55]:
Expected /Do you really want to friend sam?/ to match "<!DOCTYPE html>\n<html>\n

but the code does match, here's the test:

should "ask if you really want to friend the user" do
    get :new, friend_id: users(:samwise)
    assert_match /Do you really want to friend #{users(:samwise).first_name}?/, response.body
  end

and here's the new.html.erb

<p>
Do you really want to friend <%= @friend.first_name %>?
</p>

I tried with full_name too.

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Try changing "users(:samwise)" to "users(:samwise).profile_name" in your controller test and let us know if that works.