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

Having a problem when creating in accepting user friendships

I have an error I'm really stumped on. I'm getting an error message

"UserFriendshipsControllerTest#test: #accept when logged in should update state to accepted. :SystemStackError: stack level too deep"

from what I can tell a stack level too deep error comes up when you have a recursive method that doesn't end, but I dont think I have that in my code.

here is the controller test for the accept method.

context "#accept" do context "when not logged in" do should "redirect to the login page" do put :accept, id: 1 assert_response :redirect end end

    context "when logged in" do
        setup do
            sign_in users(:mitch)
            @user_friendship = create(:pending_user_friendship, user: users(:mitch))
            put :accept, id: @user_friendship
            @user_friendship.reload
        end

        should "assign a user friendship" do
            #assert assigns(:user_friendship)
            assert_equal @user_friendship, assigns(:user_friendship)
        end

        should "update state to accepted" do
            assert_equal 'accepted', @user_friendship.state
        end
    end
end

and here is the actual controller method

    def accept
    @user_friendship = current_user.user_friendships.find(params[:id])
    if user_friendship.accept!
        flash[:success] = "You are now friends with #{@user_friendship.friend.email}"
    else
        flash[:error] = "the friendship could not be created"
    end
    redirect_to user_friendships_path
end

any help would be awesome. Thanks!!!

2 Answers

You're method is called accept, in the if statement you're calling accept. Should that be .save otherwise you may get that 'recursive method that doesn't end' you were talking about

Haven't done the Rails tutorials yet so just guessing

Ya I'm not entirely sure how to tackle the problem, I'll try that out though. this is the solution from the github account for tree book but even when I use this solution it gives me the same error.

 def accept
@user_friendship = current_user.user_friendships.find(params[:id])
if @user_friendship.accept!
  flash[:success] = "You are now friends with #{@user_friendship.friend.first_name}"
else
  flash[:error] = "That friendship could not be accepted."
end
redirect_to user_friendships_path

end