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 trialTodor Davchev
2,060 PointsRoR: Creating the Friendship error
I saw a similar discussion on the forum but the solution to it did not apply in my case. I am getting 2 errors which I cannot understand. The errors are as follow:
1) Error:
test: #create with a valid friend_id should assign a friend object. (UserFriendshipsControllerTest):
NoMethodError: undefined method user_friendships' for nil:NilClass
/var/lib/stickshift/516e61bb5004467313000067/app-root/data/475097/app/controllers/user_friendships_controller.rb:20:in
create'
/var/lib/stickshift/516e61bb5004467313000067/app-root/data/lib/ruby/gems/gems/actionpack-3.2.6/lib/action_controller/metal/implicit_render.rb:4:in send_action'
.....
2) Error:
test: #create with a valid friend_id should assign a user_friendship object. (UserFriendshipsControllerTest):
NoMethodError: undefined method
user_friendships' for nil:NilClass
.....
Now those are my user_friendships_controller_test.rb file:
require 'test_helper'
class UserFriendshipsControllerTest < ActionController::TestCase context "#new" do context "when not logged in" do should "redirect to the login page" do get :new assert_response :redirect end end
context "when logged in" do
setup do
sign_in users(:jason)
end
should "get new and return success" do
get :new
assert_response :success
end
should "set a flash error if the friend_id params is missing" do
get :new, {}
assert_equal "Friend required", flash[:error]
end
should "display the friend's name" do
get :new, friend_id: users(:jim)
assert_match /#{users(:jim).full_name}/, response.body
end
should "assign a new user friendship to the correct friend" do
get :new, friend_id: users(:jim)
assert_equal users(:jim), assigns(:user_friendship).friend
end
should "assign a new user friendship to the currently logged in user" do
get :new, friend_id: users(:jim)
assert_equal users(:jason), assigns(:user_friendship).user
end
should "return a 404 if no friend was found" do
get :new, friend_id: 'invalid'
assert_response :not_found
end
#should "ask if you really want to friend the user" do
# get :new, freind_id: users(:jim)
# assert_match /Do you really want to friend #{users(:jim).full_name}?/, response.body
#end
end
end
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(:jason)
end
end
context "with no freind_id" do
setup do
post :create
end
should "set the flash error message" do
assert !flash[:error].empty?
end
should "set redirect to root" do
assert_redirected_to root_path
end
end
context "with a valid friend_id" do
setup do
post :create, friend_id: users(:mike).id
end
should "assign a friend object" do
assert_equal users(:mike), assigns(:friend)
end
should "assign a user_friendship object" do
assert assigns(:user_friendship)
assert_equal users(:jason), assigns(:user_friendship).user
assert_equal users(:mike), assigns(:user_friendship).friend
end
end
end
end
and my user_friendships_controller.rb file :
class UserFriendshipsController < ApplicationController before_filter :authenticate_user!, only: [:new] def new if params[:friend_id] @friend = User.where(profile_name: params[:friend_id]).first #logger.info "Trying to find a friend:" #logger.info @friend raise ActiveRecord::RecordNotFound if @friend.nil? @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)
if @user_friendship.save
flash[:success] = "Friendship created."
else
flash[:error] = "There was a problem."
end
redirect_to profile_path(@friend)
else
flash[:error] = "Friend required"
redirect_to root_path
end
end
end
I read in the other post's discussion that you suggest changing "post :create, friend_id: users(:mike).id" with "post :create, friend_id: users(:mike).profile_name" and the person querying the question's case is "post :create, friend_id: users(:mike)". I have tried all three of them and I keep getting the same errors.
Regards,
Todor Davchev
1 Answer
Yonatan Schultz
12,045 PointsLooks like you might be trying to call user_friendships' (notice the trailing apostrophe).
Rod Leviton
1,207 PointsRod Leviton
1,207 PointsI had the same issue and it seems the error is due to the tests being outside the correct context. You have placed an end tag too early after context "when logged in" do :
This is what you have:
should be: