Ruby on rails 删除';朋友';这增加了使用自引用关联的功能
我可以实现反向关系,所以如果UserA添加UserB,那么它会在B的配置文件中显示UserA,反之亦然。 但是如果UserA添加了UserB,我就不知道如何让UserB将UserA作为朋友删除。 我尝试过很多不同的方法,但每次我改变一些东西,它就会把问题转移到别处!我不知道根本问题是否是:Ruby on rails 删除';朋友';这增加了使用自引用关联的功能,ruby-on-rails,Ruby On Rails,我可以实现反向关系,所以如果UserA添加UserB,那么它会在B的配置文件中显示UserA,反之亦然。 但是如果UserA添加了UserB,我就不知道如何让UserB将UserA作为朋友删除。 我尝试过很多不同的方法,但每次我改变一些东西,它就会把问题转移到别处!我不知道根本问题是否是: a。FriendsController销毁方法是如何定义的 b。我是否需要另一个控制器专门处理 逆折射破坏 c。如果我需要自定义路线 d。如果上面所有的都可以,但是我的视图中的代码(特别是 _建议的_连接部
- a。FriendsController销毁方法是如何定义的
- b。我是否需要另一个控制器专门处理 逆折射破坏
- c。如果我需要自定义路线
- d。如果上面所有的都可以,但是我的视图中的代码(特别是 _建议的_连接部分)调用了错误的控制器 和/或路线
- e。或者以上都没有
class FriendshipsController < ApplicationController
def destroy
@friendship = current_user.friendships.find(params[:id])
@friendship.destroy
flash[:notice] = "Removed friendship."
redirect_to current_user
end
您的主要问题是: a。FriendsController销毁方法是如何定义的 您正在
当前用户中查找友谊
。友谊
,但它不在那里。这是在反向的友谊中
您需要检查这两个关联,或者让控制器知道您正在寻找哪个关联。后者可能更可取,因为尽管它们是同一类,但它们是不同的资源。可能是这样的:
# In routes, route inverse friendships to the same controller, but with a
# different path (I'm routing everything here, you may not need that.)
resources :friendships
resources :inverse_friendships, :controller => 'friendships'
# Then in your friendships controller, use the path to determine which
# collection you're working with:
#
def destroy
@friendship = collection.find(params[:id])
# ...
end
# the other collection methods would use the same collection, if you needed them,
# for example:
def create
@friendship = collection.build(params[:friendship])
# ..
end
protected
# simple case statement here, but you get the idea
def collection
case request.path
when /\/inverse_friendships/ then current_user.inverse_friendships
else current_user.friendships
end
end
最后,在你看来,你会走向一种相反的友谊,比如:
<%= link_to "remove", inverse_friendship_path(friendship), :method => :delete %>
您的主要问题是: a。FriendsController销毁方法是如何定义的 您正在
当前用户中查找友谊
。友谊
,但它不在那里。这是在反向的友谊中
您需要检查这两个关联,或者让控制器知道您正在寻找哪个关联。后者可能更可取,因为尽管它们是同一类,但它们是不同的资源。可能是这样的:
# In routes, route inverse friendships to the same controller, but with a
# different path (I'm routing everything here, you may not need that.)
resources :friendships
resources :inverse_friendships, :controller => 'friendships'
# Then in your friendships controller, use the path to determine which
# collection you're working with:
#
def destroy
@friendship = collection.find(params[:id])
# ...
end
# the other collection methods would use the same collection, if you needed them,
# for example:
def create
@friendship = collection.build(params[:friendship])
# ..
end
protected
# simple case statement here, but you get the idea
def collection
case request.path
when /\/inverse_friendships/ then current_user.inverse_friendships
else current_user.friendships
end
end
最后,在你看来,你会走向一种相反的友谊,比如:
<%= link_to "remove", inverse_friendship_path(friendship), :method => :delete %>
谢谢1311407号,这似乎有道理。我会尝试一下,让你知道它是否管用。在听从你的建议后,我仍然无法让它管用。最后,我为反向友谊创建了一个单独的控制器,成功了。好吧,这里有点尴尬。你是对的,它确实有效,尽管我需要稍微调整视图中的代码。无论如何,为帮助干杯。谢谢1311407号,这似乎是有道理的。我会尝试一下,让你知道它是否管用。在听从你的建议后,我仍然无法让它管用。最后,我为反向友谊创建了一个单独的控制器,成功了。好吧,这里有点尴尬。你是对的,它确实有效,尽管我需要稍微调整视图中的代码。无论如何,为你的帮助干杯。
<%= link_to "remove", inverse_friendship_path(friendship), :method => :delete %>
<%= link_to "remove", friendship, :method => :delete %>
OR
<%= link_to "remove", friendship_path(friendship), :method => :delete %>
def destroy
id, cid = params[:id], current_user.id
# search both associations (two queries)
@friendship = current_user.friendships.find_by_id(id) ||
current_user.inverse_friendships.find(id)
# or query friendship looking for both types
@friendship = Friendship.
where("user_id = ? OR friend_id = ?", cid, cid).find(id)
# ...
end