公共活动上的未定义方法`destroy'

时间:2014-04-13 17:42:35

标签: ruby-on-rails methods ruby-on-rails-4

用户可以在屏幕上发表评论,并由PublicActivity跟踪:

@comment.create_activity :create, owner: current_user, recipient: @comment.screen.user

并且注释依赖于::在屏幕模型上销毁。

但是当我删除屏幕时,删除评论时,该评论的PublicActivity记录仍然存在。

这是我的Screens Controller

  def destroy
    @activity = PublicActivity::Activity.find_by_trackable_id(params[:id])
    @activity.destroy #<-- Heres the Problem
    @screen.destroy
    respond_to do |format|
      format.html { redirect_to root_path }
      format.json { head :no_content }
    end
  end

但是在删除一个屏幕时,我得到的是undefined method destroy'为nil:NilClass`。

我在Railscast上看到了:

  

这是由于在对象之后调用create_activity方法   被摧毁了。

     

根据gem维护者的说法,你只需要假设记录   将被销毁,并在销毁

之前调用create_activity

我缺少什么?

  

以下信息

screen.rb

belongs_to :user
has_many :comments, :dependent =>  :destroy

comment.rb

belongs_to :user
belongs_to :screen

screens_contoller.rb

  def create
    @screen = current_user.screens.build(screen_params)
    respond_to do |format|
      if @screen.save
         format.html { redirect_to @screen, notice: 'You successfully uploaded your Screenshot.' }
        format.json { render action: 'show', status: :created, location: @screen }
        current_user.add_points(2, 'Points for Uploading a Screenshot')
      else
        format.html { render action: 'new' }
        format.json { render json: @screen.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @activity = PublicActivity::Activity.find_by_trackable_id(params[:id])
    @activity.destroy
    @screen.destroy
    respond_to do |format|
      format.html { redirect_to root_path }
      format.json { head :no_content }
      current_user.substract_points(1, "Substraction for Deleting a Screenshot")
    end
  end

comments_controller.rb

  def create
    @screen = Screen.find(params[:screen_id])
    @comment = current_user.comments.build(comment_params)
    @comment.screen_id = @screen.id
    respond_to do |format|
      if @comment.save
        # Create Record for Public Activity
        @comment.create_activity :create, owner: current_user, recipient: @comment.screen.user
        format.html { redirect_to @screen, notice: 'Comment was successfully created.' }
        format.json { render action: 'show', status: :created, location: @comment }
      else
        format.html { render action: 'new' }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @comment.destroy
    respond_to do |format|
      @activity = PublicActivity::Activity.find_by_trackable_id(params[:id])
      @activity.destroy
      format.html { redirect_to :back }
      format.json { head :no_content }
    end
  end

这就是我的屏幕控制器销毁行动现在的样子:

  def destroy
    @screen = current_user.screens.find(params[:id])
    @activity = PublicActivity::Activity.find_by_trackable_id(params[:id])
    @activity.destroy
    @screen.destroy
    current_user.substract_points(1, "Substraction for Deleting a Screenshot")
    respond_to do |format|
      format.html { redirect_to root_path }
    end
  end

同样的错误:

enter image description here

1 个答案:

答案 0 :(得分:5)

这未经过测试,但我认为你应该这样做。

首先,您可以删除screen_controller#destroy

中对活动的引用

然后在你的comments_controller#destroy

  @comment = current_user.comments.find(params[:id])
  @activity = PublicActivity::Activity.find_by(trackable_id: (params[:id]), trackable_type: controller_path.classify)
  @activity.destroy
  @comment.destroy

应该在您回复阻止之外

接下来,在您的评论模型中,您应该执行以下操作:

#comment.rb

private

before_destroy :find_and_destroy_comments

def find_and_destroy_comments
  activity = PublicActivity::Activity.find_by(trackable_id: self.id, trackable_type: self.class.name)
  if activity.present?
    activity.destroy
  end
end

调用before_destroy方法会覆盖在dependent: :destroy期间调用的默认ruby destroy方法

让我知道这是否有效,但它应该。