PG::ForeignKeyViolation: ERROR: update or delete on table "posts" violates foreign key constraint "fk_rails_fbe88b52fa" on table "users_posts"
DETAIL: Key (id)=(10) is still referenced from table "users_posts".
def destroy
post = Post.friendly.find(params[:post_id])
user_post = GlobalID::Locator.locate_signed(params[:id])
user_post.destroy!
redirect_to post_path(post), alert: "unfollow"
end
☒ It looks like we have a bit of mixed up logic here because the post.to_sgid that you're calling is the actual post, not the users_post record.
So, in your destroy action, you would need to have something like this below. This is happening likely because you're on a index action (where you're listing out all of the liked posts) instead of the show action for a particular post like in the episode.
def destroy
post = Post.friendly.find(params[:post_id])
user_post = UserPost.find_by(post: post, user: current_user)
user_post.destroy!
redirect_to post_path(post), alert: "unfollow"
end
In this scenario, you're not really doing anything with the signed global id as it's not referring to the actual UserPost.
How do I scope for posts liked by a user and show them on the user's profile page?
none of them gives me the results
You can then call user.liked_posts on your instance of the user.
## user_profile.html.erb
## users_controller
This setup works fine on the post-show page but not on the user profile page
So, in your destroy action, you would need to have something like this below. This is happening likely because you're on a index action (where you're listing out all of the liked posts) instead of the show action for a particular post like in the episode.
In this scenario, you're not really doing anything with the signed global id as it's not referring to the actual UserPost.