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!
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
LS LS
255 PointsBuilding Rails User_Friendships based on User IDs instead of Profile Names
Hi, I am trying to complete the "Programming_Building Social Features in Ruby on Rails_Building the Friendship UI _Creating the Friendship Form" lesson. I want to not use "profile_name" as the identifier for the friend but rather just use the "user_id". Does anyone know what changes need to be made in the lesson to accomodate this? I have tried leaving out the addition of "def to_param; profile_name end" in the model/user.rb file as well as changing "profile_name" to "user_id" in the user_friendships controller...but my create user_friendship code in the user_friendship controller still is not working. Many thanks, Lisa
5 Answers

LS LS
255 PointsHi again, I figured it out. The user_id was not being passed through in the form correctly. So in the new user_friendship form, I had to make the following change: <%= form.hidden_field :friend_id, value: @friend.profile_name %> to <%= form.hidden_field :friend_id, value: @friend.id %> Note that I had tried: <%= form.hidden_field :friend_id, value: @friend.user_id %> but it had not worked...still not sure why, but at least .id works. Thanks again for your interest in helping! Best, Lisa

LS LS
255 PointsHi Twiz T,
Thanks for your response. Error is below along with my UserFriendship Controller text.
Thanks, Lisa
UserFriendship Controller text:
class UserFriendshipsController < ApplicationController before_filter :authenticate_user!, only: [:new]
def new
if params[:friend_id]
@friend = User.find(params[:friend_id])
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.find(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.name}"
redirect_to show_path(@friend)
else
flash[:error] = "Friend Required"
redirect_to root_path
end
end
end
ERROR: ActiveRecord::RecordNotFound in UserFriendshipsController#create
Couldn't find User with id=#<User:0x00000102ee0c70> Rails.root: /Users/lschirf/code/ulinks
Application Trace | Framework Trace | Full Trace app/controllers/user_friendships_controller.rb:18:in `create' Request
Parameters:
{"utf8"=>"?", "authenticity_token"=>"U4vi3CJJyDbaJrMOLRIPqkfYjP6aoqzxjE5f2AcGCDw=", "user_friendship"=>{"friend_id"=>"#<User:0x00000102ee0c70>"}, "commit"=>"Yes, Add Friend"} Show session dump
Show env dump
Response
Headers:
None

LS LS
255 PointsHi again,
The error from the terminal window is below. I think it is clear that the User_ID is not flowing through correctly but I can't figure out how to fix this. Any ideas would be greatly appreciated.
Thanks, Lisa
Started POST "/user_friendships" for 127.0.0.1 at 2013-07-08 13:28:14 -0400 Processing by UserFriendshipsController#create as HTML Parameters: {"utf8"=>"?", "authenticity_token"=>"U4vi3CJJyDbaJrMOLRIPqkfYjP6aoqzxjE5f2AcGCDw=", "user_friendship"=>{"friend_id"=>"#<User:0x00000102ee0c70>"}, "commit"=>"Yes, Add Friend"} User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", "#<User:0x00000102ee0c70>"]] Completed 404 Not Found in 22ms
ActiveRecord::RecordNotFound (Couldn't find User with id=#<User:0x00000102ee0c70>): app/controllers/user_friendships_controller.rb:18:in `create'

Twiz T
12,588 PointsNo problem, wish I could have been of more help. Sometimes it just helps to talk it out (even with yourself) :)

LS LS
255 PointsAgreed! :) Thxs again!
Twiz T
12,588 PointsTwiz T
12,588 PointsCan you post what exactly you mean by not working? For example the error where your application is raising exceptions