Reputation: 3197
I have an app that allows users to follow/unfollow other users. This relationship is stored in the relationships
table, which has the fields id
, follower_id
, and followed_id
. When I call the destroy
method to unfollow a user, it destroys the relationship, but then tries to redirect back to the followed user by using the destroyed relationships id
and not the users id
. This id
is/was stored in the followed_id
field of the relationships table. I don't know how to trouble shoot this in rails.
Here is the relationship controller
class RelationshipsController < ApplicationController
def create
@relationship = Relationship.new
@relationship.followed_id = params[:followed_id]
@relationship.follower_id = current_user.id
if @relationship.save
redirect_to User.find params[:followed_id]
else
flash[:error] = "Couldn't Follow"
redirect_to root_url
end
end
def destroy
@relationship = Relationship.find(params[:id])
@relationship.destroy
redirect_to user_path params[:id]
end
end
Upvotes: 0
Views: 80
Reputation: 6015
def destroy
@relationship = Relationship.find(params[:id])
@followed_user_id = @relationship.followed_id
@relationship.destroy
redirect_to user_path @followed_user_id
end
Hope this may help :)
Upvotes: 1
Reputation: 115511
replace:
redirect_to user_path params[:id]
with:
redirect_to user_path(@relationship.followed_id)
@relationship
is removed from db but you still have the object in memory.
Upvotes: 3