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 Building Social Features in Ruby on Rails Building the Friendship UI Saving Models and Redirecting

Robert Goddard
Robert Goddard
15,019 Points

Building the Friendship UI: Code Challenge 1/3

This is very aggrivating. The following code is not working:

class UserFriendshipsController < ApplicationController
  def create
   @friend = User.where(profile_name: params[:user_friendship][:friend_id]).first
   @user_friendship = UserFriendship.new(friend: @friend, user: current_user)
   if @user_friendship.save
     flash[:success] = "Friendship created."
   end
  end
end

Link to the challenge: http://teamtreehouse.com/library/building-social-features-in-ruby-on-rails-2/building-the-friendship-ui/saving-models-and-redirecting

This is pasted directly from my "follow-along" code and it's working fine with my unit tests:

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] = "New friend, #{@friend.full_name}, added!"
      redirect_to profile_path(@friend)
    else
      flash[:error] = "Friend Required"
      redirect_to root_path
    end
  end
Robert Goddard
Robert Goddard
15,019 Points

I was able to pass the test after finding some code in another thread. This is a terrible code challenge and needs to be reworked. There is not enough information for what's requested of a student within the first question. That's ridiculous.

if params[:friend_id]
      @friend = User.find(params[:friend_id])
      @user_friendship = current_user.user_friendships.new(friend: @friend)
      if @user_friendship.save
        flash[:success] = "Friendship created."
        redirect_to profile_path(@friend)
      else
        flash[:error] = "There was a problem."
        redirect_to profile_path(@friend)
      end
    end

1 Answer

Thank you Robert -- your code worked for me. I agree that this code exercise needs work and have seen other threads calling for the same.

Most frustrating to me was that my actual, working controller code was not doing the trick. I assume there are unit tests behind the code exercise validation -- it would be great to be able to view the validation code when one is having issues to ensure that the proper variables, etc., are being populated.