喜欢评论时如何修复ActiveRecord :: RecordNotFound?

时间:2015-06-24 02:03:24

标签: ruby-on-rails ruby comments

当用户试图喜欢评论时,他遇到了这个错误:

<script>
$("nav ul li").click(function(){
    $("section.s-h").css("display", "none");
    $($(this).data("target")).css("display", "block");
});
</script>

我一直在修改控制器中的一条线,但我不确定这是不是答案。

comments_controller

ActiveRecord::RecordNotFound (Couldn't find Comment with 'id'=3 [WHERE "comments"."user_id" = ?]):
  app/controllers/comments_controller.rb:58:in `set_comment'

comment_like.rb

class CommentsController < ApplicationController
  before_action :set_commentable, only: [:index, :new, :create]
  before_action :set_comment, only: [:edit, :update, :destroy, :like]
  before_action :correct_user, only: [:edit, :update, :destroy]

  def index
    @comments = @commentable.comments
  end

  def new
    @comment = @commentable.comments.new
  end

  def create
    @comment = @commentable.comments.new(comment_params)
    if @comment.save
      redirect_to @commentable, notice: "Comment created."
    else
      render :new
    end
  end

  def edit
  end

  def update
    if @comment.update_attributes(comment_params)
      redirect_to :back, notice: "Comment was updated."
    else
      render :edit
    end
  end

  def destroy
    @comment.destroy
    redirect_to @comment.commentable, notice: "Comment destroyed."
  end

  def like
    @comment = Comment.find(params[:id]) # I've been tinkering with this line.
    @comment_like = current_user.comment_likes.build(comment: @comment)
    if @comment_like.save
      @comment.increment!(:likes)
      flash[:success] = 'Thanks for liking!'
    else
      flash[:error] = 'Too many likes'
    end  
    redirect_to(:back)
  end

  private

  def set_commentable
    @commentable = find_commentable
  end

  def set_comment
    @comment = current_user.comments.find(params[:id])
  end

  def correct_user
    @comment = current_user.comments.find_by(id: params[:id])
    redirect_to root_url, notice: "Not authorized to edit this comment" if @comment.nil?
  end

  def find_commentable
    if params[:goal_id]
      Goal.find(params[:goal_id])
    elsif params[:habit_id]
      Habit.find(params[:habit_id])
    elsif params[:valuation_id]
      Valuation.find(params[:valuation_id])
    elsif params[:stat_id]
      Stat.find(params[:stat_id])
    end
  end

  def comment_params
    params[:comment][:user_id] = current_user.id
    params.require(:comment).permit(:content, :commentable, :user_id, :like)
  end
end

comment.rb

class CommentLike < ActiveRecord::Base
  belongs_to :user
  belongs_to :comment
  belongs_to :habit
  belongs_to :stat
  belongs_to :valuation
  belongs_to :goal
  validates :user, uniqueness: { scope: :comment }
  belongs_to :liker, class_name: 'User', foreign_key: :user_id
  belongs_to :liked_comment, class_name: 'Comment', foreign_key: :comment_id
end

模式

class Comment < ActiveRecord::Base
    after_save :create_notification
  has_many :notifications
  has_many :comment_likes   
  has_many :likers, through: :comment_likes, class_name: 'User', source: :liker
  belongs_to :habit
  belongs_to :stat
  belongs_to :valuation
  belongs_to :goal
    belongs_to :user
  validates :user, presence: true

private

  def create_notification
    author = 
      if goal
        goal.user
      #elsif comment_like
      # comment_like.user
      elsif habit
       habit.user
      elsif stat
        stat.user
      elsif valuation
        valuation.user
      end
    notifications.create(
      comment:      self,
      likes:        likes,
      habit:        habit,
      stat:         stat,
      goal:         goal,
      valuation:    valuation,
      user:         author,      
      read:         false
    )
  end
end

其他一切都有效,例如评论和喜欢用户自己的评论的能力,但不是当用户试图喜欢别人的评论时。

2 个答案:

答案 0 :(得分:1)

此错误是由

引起的
before_action :set_comment, only: [:edit, :update, :destroy, :like]

删除:like将解决问题。

原帖的其他代码行很好。

由于Comment喜欢的current_user不属于current_usercurrent_user.comments.find(params[:id])会引发ActiveRecord::RecordNotFound

答案 1 :(得分:1)

根据文档here,如果找不到记录,则会抛出ActiveRecord::RecordNotFound异常。

错误消息指出app/controllers/comments_controller.rb:58:in 'set_comment'set_comment似乎被调用,因为您告诉控制器将其称为before_action

before_action :set_comment, only: [:edit, :update, :destroy, :like]

set_comment执行以下操作

@comment = current_user.comments.find(params[:id])

它只是在current_user的评论范围内,这解释了为什么用户可以喜欢他自己的评论而不是其他人的评论。您需要将其更改为

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

现在它将查找具有该ID的所有评论。此外,这意味着您可以删除like操作的第一行,因为它与set_comment完全相同,这是多余的(您要查询数据库两次)。