Ruby on rails Rails:can ActiveRecord方法";“销毁”;出问题而不被执行?

Ruby on rails Rails:can ActiveRecord方法";“销毁”;出问题而不被执行?,ruby-on-rails,rspec,Ruby On Rails,Rspec,我正在RoR中编写一个应用程序,我想知道是否应该在其中添加以下if语句: class ArticlesController < ApplicationController before_action :set_user, only: %i[destroy edit] ... def destroy if @article.destroy flash[:success] = 'Article deleted' redirect_to articles_

我正在RoR中编写一个应用程序,我想知道是否应该在其中添加以下if语句:

class ArticlesController < ApplicationController
  before_action :set_user, only: %i[destroy edit]
...
  def destroy
    if @article.destroy
      flash[:success] = 'Article deleted'
      redirect_to articles_path
    else
      flash[:error] = 'Failed to delete the article'
      redirect_to article_path(@article)
    end
  end
...

private

  def set_user
    @article = Article.find(params[:id])
    if current_user.id == @article.author_id
      @author = current_user
    else
      redirect_to article_path(@article), message: 'You can\'t do that'
    end
  end
end
class-ArticlesController
是否可以调用“else”分支i destroy方法?我正在为那个控制器编写规范,我想不出测试代码的方法。 我使用了与“.save”相同的方法,但我们正在处理DB验证。如果无法调用“else”,我将删除这些行


我发现搜索文章所有者或按id查找文章时出错,错误将出现在前面的“set\u user”方法中。

这里有一个类似的问题:如果文章不存在,它无法销毁文章,或者您可以添加其他内容restrictions@FranMartinez谢谢这正是我想要的答案:)