Rails 3 friendship model : how to ignore a friend request?

早过忘川 提交于 2019-12-04 19:22:04

I'm editing my whole answer because I think I found the gist of your problem. When a user tries to be friends with someone, you add a relationship to that user but you don't add it to the other user. SO, current_user.friendships.find(params[:id]) expects the current_user to have a friendship with that id but that relationship doesn't belong to him, but to the other user.

I'm not sure if I made it clear enough, but here's my 2nd try:

Your link should be:

<%= link_to "Ignore", friendship_path(:id => friendship_id, :friend_id => user.id), :method => :delete  %>)

And then your action:

def destroy
  potential_friend = User.find(params[:friend_id])
  friend_request = potential_friend.friendships.find(params[:id])

  friendship = current_user.friendships.find_by_friend_id(potential_friend.id)
  if friendship.nil?      #Users are not friends and you want to delete the friend request
    friend_request.destroy
    flash[:notice] = "Removed friend request."
  else                    #Users are friends and you want to delete the friendship
    friendship.destroy
    friend_request.destroy
    flash[:notice] = "Removed friendship."
  end
  redirect_to current_user
end

I'm not sure if you should turn this into custom action. As you can see, it does much more than just destroy a single object.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!