Rails 3 友谊模型:如何忽略好友请求? [英] Rails 3 friendship model : how to ignore a friend request?

查看:89
本文介绍了Rails 3 友谊模型:如何忽略好友请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有传统的友谊模式:

用户模型具有:

has_many :friendships, :dependent => :destroy
  has_many :friends, :through => :friendships, :dependent => :destroy
  has_many :inverse_friendships, :class_name => "Friendship", :foreign_key => "friend_id", :dependent => :destroy
  has_many :inverse_friends, :through => :inverse_friendships, :source => :user, :dependent => :destroy

我想仅在已发送好友请求并被其他用户接受时才定义状态好友".一切正常,只是我没有设法处理忽略好友请求"部分.

I would like to define the status "friend" only if a friend request has been sent, and accepted by the other user. Everything works fine except that I did not manage to process the "ignore friend request" part.

我想要做的是:在用户个人资料页面上,有一个来自其他用户的请求列表,正在等待批准.然后,用户可以接受好友请求(然后成为好友)或拒绝它(然后破坏好友关系).

What I am trying to do is: on a user profile page, there's a list of requests from other user, waiting approval. The user can then accept a friend request (then it becomes a friend) or reject it (then the friendship relationship is destroyed).

这是友谊控制器中的一段代码,当它阻塞时:

Here is the piece of code, in the friendship controller, when it blocks:

<h2 class="small_v_space">Waiting your approval</h2>
<ul>
  <% for user in @user.inverse_friends %>
    <% if user.friends.exists?(@user) and not @user.friends.exists?(user) %>
      <li>
        <%=h user.name %>
          (<%= link_to "Accept", friendships_path(:friend_id => user), :method => :post %>,
           <%= link_to "Ignore", friendship_path, :controller => :friendships, :method => :delete  %>)
      </li
    <% end %>
  <% end %>
</ul>

问题是,如果我这样做,delete 方法将删除最后添加的关系,而不是链接到忽略按钮的关系.

The problem is that if I do like this, the delete method will delete the last added relationship, instead of the one linked to the ignore button.

让我们看一个例子:这是我想破坏的关系:用户 ID:10Friend_id: 6Friendship_id:18

Let's work with an example: Here is the relationship I would like to destroy: User_id: 10 Friend_id: 6 Friendship_id: 18

我在 user_id 为 6 的用户的显示"页面(个人资料)上.我看到用户 10 提出了一个我想忽略的好友请求.即使我设法提取了正确的 Friendship_id,做:

I'm on the 'show' page (profile) of user, whose user_id is 6. I see that user 10 has made a friend request that I would like to ignore. Even if I managed to extract the proper Friendship_id, doing:

<%= link_to "Ignore", friendship_path(Friendship_id), :controller => :friendships, :method => :delete  %>)

结果是找不到 ID=18[where user_id=6] 的好友"

It results in "Cannot find Friendships with ID=18[where user_id=6]"

有谁知道在这种情况下我可以如何对正确的关系调用销毁操作?或者我应该采取不同的方式?

Does anyone know how I could call the destroy action on the right relationship in this case ? Or should I proceed differently ?

非常感谢您提供任何线索!

Many thanks for any clue!

编辑

销毁好友控制器的动作:

def destroy
    @friendship = current_user.friendships.find(params[:id])
    if @friendship.friend.friends.exists?(current_user)
      @friendship.destroy
      flash[:notice] = "Removed friendship."
    else
      @friendship.destroy
      flash[:notice] = "Removed friend request."
    end
    redirect_to current_user
  end

编辑 2:

class Friendship
   belongs_to :user
   belongs_to: :friend, :class_name => 'User'
 end

用户 A 向用户 B 发送好友请求(在类的实例中,A=用户,B=好友).如果 B 接受请求,则创建另一个实例(B=user,A=friend).如果同时存在关系 A->B 和 B->A,则 A 是 B 的朋友.否则,请求保持挂起(或可以忽略、拒绝......).

User A sends a friend request to user B (A=user, B=friend in the instance of the class). If B accepts the requests, then another instance is created (B=user, A=friend). If there exists both a relation A->B and B->A, A is a friend of B. Otherwise, the request remains pending (or can be ignored, rejected...).

推荐答案

我正在编辑我的整个答案,因为我想我找到了您的问题的要点.当用户尝试与某人成为朋友时,您会向该用户添加关系,但不会将其添加到其他用户.所以,current_user.friendships.find(params[:id]) 期望 current_user 与该 id 建立友谊,但这种关系不属于他,而是属于另一个用户.

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:

您的链接应该是:

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

然后你的行动:

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.

这篇关于Rails 3 友谊模型:如何忽略好友请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆