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
Yulai Fjeld
680 PointsMutual Friendships - Cannot accept friendship
I'm currently going through the 'treebook' tutorial, and having a problem with accepting friend requests. I successfully have managed to send friend requests, having them show up as requested friendships, but once the user clicks on the link 'Accept Friendship', I get the following error:
ActiveRecord::RecordNotFound in UserFriendshipsController#edit
Couldn't find UserFriendship with id=test1 [WHERE "user_friendships"."user_id" = 5]
app/controllers/user_friendships_controller.rb:47:in `edit'
Where test1 is a test user created to friend another user. My edit method in the user_friendships_controller.rb looks like this:
def edit
@user_friendship = current_user.user_friendships.find(params[:id])
@friend = @user_friendship.friend
end
Any help is highly appreciated!
Note: the same thing also happens when attempting to delete a request.
13 Answers
Drake Ramming
5,373 PointsDid you clear friendships created from in-browser testing in the console?
$ bin/rails console
> UserFriendship.delete_all
Yulai Fjeld
680 PointsI did that, but still get the following message:
ActiveRecord::RecordNotFound in UserFriendshipsController#edit
Couldn't find UserFriendship with id=YulaiFjeld [WHERE "user_friendships"."user_id" = 5]
app/controllers/user_friendships_controller.rb:47:in `edit'
Parameters:
{"id"=>"YulaiFjeld"}
This time tried creating a new user and sending the user a friend request from myself ("YulaiFjeld").
Line 47 in the user_friendships_controller.rb looks like:
@user_friendship = current_user.user_friendships.find(params[:id])
Drake Ramming
5,373 PointsCould you please provide your mutual_friendship and accept_mutual_friendship! definitions from models > user_friendship.rb
Yulai Fjeld
680 PointsI appreciate the help! Here they are:
def mutual_friendship
self.class.where({user_id: friend_id, friend_id: user_id}).first
end
def accept_mutual_friendship!
# Grab the mutal friendship and update the state without using
# the state machine so as not to invoke callbacks.
mutual_friendship.update_attribute(:state, 'accepted')
end
Drake Ramming
5,373 PointsI'm sorry, I haven't been much help thus far. I just finished this section, so thought I'd give it a shot while it was still fresh in my mind.
Could you just provide your entire user_friendships_controller code?
Yulai Fjeld
680 PointsDon't worry. Still very much appreciated. I have been trying to figure out what's wrong, but can't seem to get it. Here is the entire user_friendships_controller.rb code:
class UserFriendshipsController < ApplicationController
before_filter :authenticate_user!
def index
@user_friendships = current_user.user_friendships.all
end
def accept
@user_friendship = current_user.user_friendships.find(params[:id])
if @user_friendship.accept!
flash[:success] = "You are now friends with #{@user_friendship.friend.full_name}!"
else
flash[:error] = "Friendship could not be accepted."
end
redirect_to user_friendships_path
end
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 = UserFriendship.request(current_user, @friend)
if @user_friendship.new_record?
flash[:error] = "There was a problem with the request."
else
flash[:success] = "Friend request sent."
end
redirect_to profile_path(@friend)
else
flash[:error] = "Friend required"
redirect_to root_path
end
end
def edit
@user_friendship = current_user.user_friendships.find(params[:id])
@friend = @user_friendship.friend
end
end
Yulai Fjeld
680 PointsAlso, when I click on "Accept Friendship", I get the following message in the terminal window:
Started GET "/user_friendships/test_friend/edit" for 127.0.0.1 at 2013-04-15 01:22:06 +0200
Processing by UserFriendshipsController#edit as HTML
Parameters: {"id"=>"test_friend"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 7 LIMIT 1
UserFriendship Load (0.1ms) SELECT "user_friendships".* FROM "user_friendships" WHERE "user_friendships"."user_id" = 7 AND "user_friendships"."id" = ? LIMIT 1 [["id", "test_friend"]]
Completed 404 Not Found in 2ms
ActiveRecord::RecordNotFound (Couldn't find UserFriendship with id=test_friend [WHERE "user_friendships"."user_id" = 7]):
app/controllers/user_friendships_controller.rb:47:in `edit'
Drake Ramming
5,373 PointsOkay, so it's passing in the profile name as the user_id parameter. I am not sure how that is happening.
Drake Ramming
5,373 PointsEnsure you are properly sending in your parameters from your user_friendship forms.
Yulai Fjeld
680 PointsThis is my user_friendships/index.html.erb
<div class="page-header">
<h1>Friends</h1>
</div>
<% @user_friendships.each do |friendship| %>
<% friend = friendship.friend %>
<div id="<%= dom_id(friendship) %>" class="friend row">
<div class="span1">
<%= link_to image_tag(friend.gravatar_url), profile_path(friend) %>
</div>
<div class="span7">
<strong><%= friend.full_name %></strong><br />
<% if friendship.pending? %>
<em>Friendship is pending.</em> <%= link_to "Delete request", edit_user_friendship_path(friendship.friend.profile_name) %>.
<% end %>
<% if friendship.requested? %>
<em>Friendship requested.</em> <%= link_to "Accept Friendship", edit_user_friendship_path(friendship.friend.profile_name) %>.
<% end %>
<% if friendship.accepted? %>
<em>Friendship started <%= friendship.updated_at %>.</em> <%= link_to "Update friendship", edit_user_friendship_path(friendship.friend.profile_name) %>.
<% end %>
</div>
</div>
<% end %>
Drake Ramming
5,373 PointsI think we may have found the problem. So you have your friend variable which you are declaring as friend = friendship.friend
You should be using this friend variable as the argument for the edit_user_friendship_path, as opposed to friendship.friend.profile_name
<%= link_to "Delete request", edit_user_friendship_path(friendship) %>
<%= link_to "Accept friendship", edit_user_friendship_path(friendship) %>
<%= link_to "Update friendship", edit_user_friendship_path(friendship) %>
Give that a try and see if that fixes your error.
Yulai Fjeld
680 PointsYou are right! Thank you so much for all the help! I'm still a bit confused, as to why I had the friendship.friend.profile_name there in the first place. But thank you very much for taking the time to help me!
Drake Ramming
5,373 PointsNo problem. Sorry we weren't able to work through it more quickly, just difficult when you can't see the entire codebase.
I would probably go back to the section of the tutorial where you create the index and work forward checking/debugging your code. This will ensure that all of your tests and code is correct as it would be even more difficult to have to debug in the future.