我似乎无法让Amistad友谊正常工作。我收到以下错误:如何创建好友?
ActiveRecord::RecordNotFound in FriendshipsController#update
Couldn't find Friendship with id=29
我也使用devise和cancan。我遵循维基页面上的创业板设置,并按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宝石的朋友方法,这里是the code for that method。至于我的Ruby日志,显示错误的部分非常长,所以我将它包含在this gist中。
你可以显示'current_user.friends'的代码,它在那里失败吧?我的意思是,在日志中,ruby抱怨哪一行? – juanpastas
@juanpastas我已更新以证明您的问题的答案 – Thomas
您的'development.log'说'ActiveRecord :: RecordNotFound(无法找到id = 32的友谊)'在您问题'update'正在执行'User.find'你能解释一下吗? – juanpastas