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
Bruce Canedy
13,496 PointsCreate a new UserFriendship instance variable set to user_friendship with the user set to the current_user and the friend set to the @friend instance variable.
I have submitted the following code, but i can't get it to work for challenge.
class UserFriendshipsController < ApplicationController
def new # Write your code here if params[:friend_id]
@friend = User.find(params[:freind_id])
@user_friendship = UserFriendship.find(params[:user_id])
else
flash[:error] = "Friend required"
end
end end
12 Answers
Seph Cordovano
17,400 PointsWow, this is a buggy test! I finally sorted it out but the below code should get you through it from start to finish. If not, just paste in and comment out the parts you don't need yet.
class UserFriendshipsController < ApplicationController
def block
@user_friendship = current_user.user_friendships.find(params[:id])
if @user_friendship.block!
flash[:success] = "boom"
end
redirect_to user_friendships_path
end
end
Jon Thomas
8,916 Points@steve, That's what I would expect to work as well, but it doesn't pass. Based on what I just learned in the tutorial, I would expect to be able to write:
@user_friendship = current_user.user_friendships.new(friend: @friend)
So, on step 3/3 for the "Finding and building models" challenge, I was expecting the final code to be:
class UserFriendshipsController < ApplicationController
def new
@friend = User.find(params[:friend_id])
@user_friendship = current_user.user_friendships.new(friend: @friend)
unless params[:friend_id]
flash[:error] = "Friendship required"
end
end
end
I cannot for the life of me get beyond this challenge.
We didn't go over anything like this in the video, but it works:
@user_friendship = UserFriendship.new(:user => current_user, :friend => @friend)
Making the final code for step 3 in the challenge:
class UserFriendshipsController < ApplicationController
def new
@friend = User.find(params[:friend_id])
@user_friendship = UserFriendship.new(:user => current_user, :friend => @friend)
unless params[:friend_id]
flash[:error] = "Friendship required"
end
end
end
Really threw me off.
ecp
838 PointsHi Bruce,
I'm afraid I don't know what code challenge you're on. We need a bit more detail from you to help. Would you please send an email to help@teamtreehouse.com with a screenshot of the code challenge as you're working on it, and a link to the code challenge?
Screenshots will help us see what quiz or code challenge your on, what it is asking you exactly, what your answer is, and what the error message or hint says to you when it pops up. Thank you so much! :)
Jody Albritton
Courses Plus Student 5,497 PointsWhat you need to do in this challenge is find out if the params[:friend_id] exists.
if param[:friend_id]
Bruce Canedy
13,496 PointsBut that is what I did.
def new
if params[:friend_id]
@friend = User.find(params[:freind_id])
@user_friendship = UserFriendship.find(params[:user_id])
else
flash[:error] = "Friend required"
end
end
Jody Albritton
Courses Plus Student 5,497 PointsOk your code passes until you get to the part where you make a new friendship.
You should do something like
@user_friendship = UserFriendship.new(:user => current_user, :friend => @friend)
instead of
@user_friendship = UserFriendship.find(params[:user_id])
Bruce Canedy
13,496 PointsThat worked, but i question should i have known that syntax at this point in the tutorial?
Jody Albritton
Courses Plus Student 5,497 PointsIt stumped me when I did it a few weeks ago. I just had to write out the command literally. The command is also written differently from the project files.
If you look in the project files you can see the full syntax for creating a user friendship.
Jason Seifer
Treehouse Guest TeacherSorry for the trouble on this one, guys! Is everything working for you now, Bruce?
Bruce Canedy
13,496 PointsYes it is working based on Jody's response. Understanding there are multiple ways to solve any problem. Would you have expected the question to be answered the way it ways?
Steve Christie
9,509 PointsHey Jason, I got it work using Bruce's code above, but not if I tried the code used in the video:
@user_friendship = current_user.user_friendships.new(friend: @friend)
chrisvoss
11,989 PointsSame problem here. Using the code used in the video gives me:
5d6156d5-fa49-4102-bc6d-f6564751ca95.rb:84:in new': undefined methoduser_friendships' for #<User:0x00000001218a08 @options={:current_user=>true}> (NoMethodError)
from 5d6156d5-fa49-4102-bc6d-f6564751ca95.rb:81:in anonymous'
from 5d6156d5-fa49-4102-bc6d-f6564751ca95.rb:139:ineval'
from 5d6156d5-fa49-4102-bc6d-f6564751ca95.rb:139:in `<main>'
Lut Garcia
3,631 PointsLut Garcia
3,631 PointsHi, maybe is the first line ...says :freind_id and should say :friend_id