【发布时间】:2013-07-27 18:30:54
【问题描述】:
我似乎无法让 Amistad 的友谊正常工作。我收到以下错误:
ActiveRecord::RecordNotFound in FriendshipsController#update
Couldn't find Friendship with id=29
我也在使用 devise 和 cancan。我按照 wiki 页面上的 gem 设置并按照this related post 中的描述创建了我的控制器。
class FriendshipsController < ApplicationController
before_filter :authenticate_user!
def index
@friends = current_user.friends
@pending_invited_by = current_user.pending_invited_by
@pending_invited = current_user.pending_invited
end
def create
@friend = User.find(params[:user_id])
@friendship_created = current_user.invite(@friend)
if @friendship_created
redirect_to users_path, :notice => "Your friend request is pending"
end
end
def update
@friend = User.find(params[:user_id])
@friends = current_user.friends
@pending_invited_by = current_user.pending_invited_by
redirect_to users_path, :notice => "You are now friends!"
end
def destroy
@friend = User.find(params[:user_id])
@friendship = current_user.send(:find_any_friendship_with, @friend)
if @friendship
@friendship.delete
@removed = true
redirect_to users_path, :notice => "You are no longer friends!"
end
end
def createblock
@friend = User.find(params[:user_id])
current_user.block @friend
redirect_to users_path, :notice => "You have blocked #{@friend.first_name}"
end
end
我以以下方式循环遍历我的用户,检查用户的当前状态并提供适当的操作。
<% if current_user.friend_with? user %>
<%= link_to "Unfriend", friend_path(user), :method => "delete", :class => 'btn btn-mini' %>
<% elsif current_user.invited? user %>
<span class="btn btn-mini disabled">Pending</span>
<% elsif user.invited? current_user %>
<%= link_to "Accept", friend_path(user), :method => "put", :class => 'request-approve btn btn-mini' %>
<%= link_to "Decline", friend_path(user), :method => "delete", :class => 'request-decline btn btn-mini' %>
<% else %>
<%= link_to "Add friend", friends_path(:user_id => user), :method => "post", :class => 'btn btn-mini' %>
<% end %>
认为在我的架构中查看友谊表的样子会很有用:
create_table "friendships", :force => true do |t|
t.integer "friendable_id"
t.integer "friend_id"
t.integer "blocker_id"
t.boolean "pending", :default => true
end
add_index "friendships", ["friendable_id", "friend_id"], :name => "index_friendships_on_friendable_id_and_friend_id", :unique => true
我理解错误只是无法弄清楚这应该如何改变。我认为我的问题是我正在传递一个朋友 ID,它正在期待一个友谊 ID。我对这个解决方案的唯一问题是我能找到的每个示例或帖子都建议传递 user_id,就像上面的这篇帖子一样,回答者指出 gem 开发人员提供了他回答的代码。
我觉得我的更新方法需要的是替换:
@friend = User.find(params[:id])
有了这个:
@friendship = Friendship.find_by_friend_id(params[:id])
编辑 我可以成功请求朋友,但我无法接受或拒绝朋友。我是用户列表,单击“添加朋友”链接会在友谊数据库中正确创建记录。如果我以最近请求的用户身份登录并尝试接受该请求,则会出现上述错误。如果我尝试拒绝请求,也会发生这种情况。
您要求查看的好友方法与 amistad gem 一起提供,这里是the code for that method。至于我的 Ruby 日志,显示错误的部分很长,所以我将其包含在 this gist 中。
【问题讨论】:
-
你能显示
current_user.friends的代码吗,它在那里失败了,对吧?我的意思是,在日志中,ruby 抱怨哪一行? -
@juanpastas 我已更新以证明您的问题的答案
-
你的
development.log在你的问题中说ActiveRecord::RecordNotFound (Couldn't find Friendship with id=32)update正在做User.find你能解释一下吗? -
我正在寻找用户并将他们放入朋友变量中,以便我可以通过下一行代码批准用户:
current_user.approve(@friend)
标签: ruby-on-rails ruby-on-rails-3 friend amistad