Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如果参数[:id]没有';不存在于数据库中_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 如果参数[:id]没有';不存在于数据库中

Ruby on rails 如果参数[:id]没有';不存在于数据库中,ruby-on-rails,ruby,Ruby On Rails,Ruby,我正在控制器中编写一个show操作,如果数据库中不存在从params[:id]收到的id,我希望进行分支 我的应用程序仍在创建中。我尝试过条件分支和异常处理。然而,它们并没有像预期的那样工作 class-ArticlesController

我正在控制器中编写一个show操作,如果数据库中不存在从
params[:id]
收到的id,我希望进行分支

我的应用程序仍在创建中。我尝试过条件分支和异常处理。然而,它们并没有像预期的那样工作

class-ArticlesController
如果发生任何错误,我希望被重定向到根目录

如果成功找到记录,上述代码将返回该记录。但是,如果未找到任何记录,则会在ArticlesController#show中出现
ActiveRecord::RecordNotFound


我应该如何编写此错误的代码?

您的代码应该这样编写:

 def show
    begin
      @article = Article.find(params[:id])
    rescue
      redirect_to root_path
    end
 end
rescue_from ActiveRecord::RecordNotFound do
   redirect_back fallback_location: root_path, alert: t("resource_not_found")
end
 def show
    @article = Article.find_by(id: params[:id])
    unless @article.present?
      flash[:error] = "Not Found."
      redirect_to root_path
    end
  end
我应该如何为此错误编写代码

简单的回答是“你不应该”

海事组织认为,例外情况应当是例外情况。听起来您可能会遇到这种情况,因此我建议您编写代码,在不引发异常的情况下处理该场景

如果您确实认为在某些情况下,给定的
参数[:id]
不存在
文章
记录(似乎有点奇怪,但我想这是可能的),那么我建议您使用
.find\u by
而不是
。find

class ArticlesController < ApplicationController

  #...

  def show
    if @article = Article.find_by(id: params[:id])
      # do something with the article 
    else
      render root
    end
  end

  #...

end
但是,也许不是

还有,拯救这样的一切:

def show
  begin
    @article = Article.find(params[:id])
  rescue
    render root
  end
end

…通常被视为代码气味。互联网上有一个关于原因的问题。

您可以从
ArticlesController
处理异常,但我建议将代码放在
ApplicationController
如下所示:

 def show
    begin
      @article = Article.find(params[:id])
    rescue
      redirect_to root_path
    end
 end
rescue_from ActiveRecord::RecordNotFound do
   redirect_back fallback_location: root_path, alert: t("resource_not_found")
end
 def show
    @article = Article.find_by(id: params[:id])
    unless @article.present?
      flash[:error] = "Not Found."
      redirect_to root_path
    end
  end

也许您可以这样编写代码:

 def show
    begin
      @article = Article.find(params[:id])
    rescue
      redirect_to root_path
    end
 end
rescue_from ActiveRecord::RecordNotFound do
   redirect_back fallback_location: root_path, alert: t("resource_not_found")
end
 def show
    @article = Article.find_by(id: params[:id])
    unless @article.present?
      flash[:error] = "Not Found."
      redirect_to root_path
    end
  end

为什么要写这么多代码,而你只能用两行代码来实现呢

class ArticlesController < ApplicationController
  def show
    @article = Article.find_by(id: params[:id])
    redirect_to root_path unless @article.present?
  end 
end 
class-ArticlesController
从答案中注意,他们都使用find_by而不是find,即使您只是检查id。这是因为如果找不到条目,find_by将返回nil。即使问了这个问题,我还是在网上寻找答案。目前,它在
Article.exists?(params[:id])
中可以正常工作。然而,你的答案正是我想要的。非常感谢你!