【问题标题】:How to allow video uploader to delete other user's comments on their videos如何允许视频上传者删除其他用户对其视频的评论
【发布时间】:2019-09-11 08:43:37
【问题描述】:

我正在尝试让视频上传者能够删除其他用户在其视频下留下的 cmets。到目前为止,我发现的所有内容都只是关于评论作者只能删除他们自己的 cmets,而不能删除其他人。

带有 Rails 后端的 React/redux。

  def destroy
    @comment = Comment.find(params[:id])

    if @comment.user_id == current_user.id 
      @comment.destroy
      render :show
    else
      render json: ['Only author may can delete comments']
    end
  end

【问题讨论】:

  • 您可以先检查 current_user 是视频所有者,然后您可以允许他们删除它
  • 这样@video = current_user.videos.where(id:params[:video_id]) 然后if @video.present? 允许删除评论
  • @MarkMerritt punditcancancan 非常适合这种功能
  • 您还需要这方面的帮助吗?

标签: javascript ruby-on-rails reactjs


【解决方案1】:

基本的逻辑变化是这样的:

def destroy
    @comment = Comment.find(params[:id])
    video = @comment.video # I assume you have an association for this

    if @comment.user_id == current_user.id || video.user_id == current_user.id
      @comment.destroy
      render :show
    else
      render json: ['Only author may can delete comments']
    end
end

【讨论】:

    猜你喜欢
    • 2023-02-06
    • 2016-01-29
    • 1970-01-01
    • 2021-10-11
    • 2015-03-15
    • 1970-01-01
    • 2022-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多