【问题标题】:How do I create friends?如何创建朋友?
【发布时间】: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


【解决方案1】:

鉴于我目前的声誉,我只能发布答案而不是评论您的问题,但据我从您发布的控制器来源中可以看出,您在调用 current_user.approve @friend您的更新操作。

我最近在我的一个项目中使用了这个 gem,没有遇到任何问题。控制器动作如下所示:

def update
  @friend = User.find_by_id(params[:id])
  if current_user.approve @friend
    redirect_to friendships_path, notice: t('.confirmation_successful')
  else
    redirect_to friendships_path, alert: t('.confirmation_unsuccessful')
  end
end

def destroy
  @friend = User.find_by_id(params[:id])
  if current_user.remove_friendship @friend
    redirect_to friendships_path, notice: t('.deletion_successful')
  else
    redirect_to friendships_path, alert: t('.deletion_unsuccessful')
  end
end

我希望这会有所帮助。

【讨论】:

    【解决方案2】:

    查找问题是因为您传递的 ID 不一致。在其中的 3 个链接中,您直接传递了 User 对象,该对象应自动将 id 存储在 params[:id] 中,您可以在操作中将其用作 User.find(params[:id])。但在您的操作中,您是从空的params[:user_id] 中提取它。不确定您如何在错误消息中获得 ID 29(或 32 或其他),但是...

    如果您将所有操作更改为期望 params[:id] 并切换“添加朋友”路径链接以像其他方式一样传入 User 对象,您应该在正确的参数中传递正确的数据,并且查找应该自己理顺。

    当然,正如 Wonky Business 指出的那样,您实际上并没有在更新方法中调用 approve,因此实际上没有任何链接,但至少您应该找到所有模型对象。

    顺便说一句,从您的路径看来,您正在将 friendship 命名路由重新映射到 friend。这使问题变得混乱,因为 RESTful 路由实际上都没有执行它们的名词/动词组合所暗示的内容:如果您使用 POST 调用 friends_path(user),则完成后应该有一个新的 Friend 对象,但这个控制器是创建和销毁 Friendship 对象并单独保留 Friend 对象。

    如果您删除该别名并切换到 friendship_path 等等,那么 REST 实际上会按照它所说的那样做:管理 friendship 对象。

    希望有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-05
      • 2011-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-21
      • 1970-01-01
      相关资源
      最近更新 更多