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
Andre' Jones
26,671 PointsCreating the Friendship
I am still getting the friend required flash message when i try to add a new friend.
user_friendships_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? @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[:user_friendship] && params[:user_friendship].has_key?(:friend_id) @friend = User.where(profile_name: params[:user_friendship][:friend_id]).first @user_friendship = current_user.user_friendships.new(friend: @friend) @user_friendship.save flash[:success] = "You are now friends with #{@friend.full_name}" redirect_to profile_path(@friend) else flash[:error]="Friend required" redirect_to root_path end
end end
U_F_C_test.rb
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(:andre)
end
should "get new and return success" do
get :new
assert_response :success
end
should "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 friends name"do
get :new, friend_id: users(:mike)
assert_match /#{users(:mike).full_name}/,response.body
end
should "assign a new user friendship" do
get :new, friend_id: users(:jim)
assert assigns(:user_friendship)
end
should "assign a new user 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 to the correct friend to the currently logged in user" do
get :new, friend_id: users(:jim)
assert_equal users(:andre), assigns(:user_friendship).user
end
should "returns a 404 status if no friend is 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, friend_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 signin_path end end
context "when logged in" do
setup do
sign_in users(:andre)
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
context "with a valid friend_id" do
setup do
post :create, user_friendship: {friend_id: users(:mike)}
end
should "assign a friend object" do
assert assigns(:friend)
assert_equal users(:mike), assigns(:friend)
end
should "assign a user friendship object" do
assert assigns(:user_friendship)
assert_equal users(:andre), assigns(:user_friendship).user
assert_equal users(:mike), assigns(:user_friendship).friend
end
should "create a friendship" do
assert users(:andre).friends.include?(users(:mike))
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 success message" do
assert flash[:success]
assert_equal "You are now friends with #{users(:mike).full_name}", flash[:success]
end
end
end
end
end end
1 Answer
Andre' Jones
26,671 PointsFigured it out had my end in the wrong place - SLOW DOWN JASON - know i can pause but u type fast and move on to the next so quickly that i miss stuff and dont know it.
But im doing a project on my own with rails and I'm sure I'll have questions so be on the look out for me
Andre' Jones
26,671 PointsAndre' Jones
26,671 Pointssorry code got jumbled up but idk how to fix it. but again I keep getting the friendship required message when i go to add a friend