[英]How should I write code if a params[:id] doesn't exist in the db
I'm writing a show action in a controller and want to branch if no id received from params[:id]
exists in my database.我正在 controller 中编写一个显示操作,如果我的数据库中不存在从
params[:id]
接收到的 id,我想分支。
My app is still being created.我的应用程序仍在创建中。 I've tried conditional branching and exception handling.
我试过条件分支和异常处理。 However, they didn't work as expected.
但是,它们并没有按预期工作。
class ArticlesController < ApplicationController
#...
def show
begin
@article = Article.find(params[:id])
rescue
render root
end
end
#...
end
I expect to be redirect to the root if any error occurs.如果发生任何错误,我希望被重定向到根目录。
The above code returns the record if it is found successfully.上面的代码如果找到成功则返回记录。 However, an
ActiveRecord::RecordNotFound in ArticlesController#show
occurs if no record is found.但是,如果未找到记录,则会
ActiveRecord::RecordNotFound in ArticlesController#show
。
How should I write code for this error?我应该如何为这个错误编写代码?
How should I write code for this error?
我应该如何为这个错误编写代码?
The short answer is "you shouldn't".简短的回答是“你不应该”。
Exceptions should, IMO, be exceptional. IMO,例外应该是例外。 It sounds like you might expect this circumstance, so I suggest you write code that handles the scenario without raising an exception.
听起来您可能会预料到这种情况,所以我建议您编写代码来处理这种情况而不会引发异常。
If you really think you'll have circumstances where the Article
record does not exist for the given params[:id]
(seems a little odd, but I guess it's possible), then I would suggest that you use .find_by
instead of .find
:如果你真的认为你会遇到给定的
params[:id]
不存在Article
记录的情况(看起来有点奇怪,但我想这是可能的),那么我建议你使用.find_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
It seems like to you might want to do a redirect
instead of a render
:在您看来,您可能想要进行
redirect
而不是render
:
class ArticlesController < ApplicationController
#...
def show
if @article = Article.find_by(id: params[:id])
# do something with the article
else
redirect_to root_path
end
end
#...
end
But, maybe not...但是,也许不是...
Also, rescuing everything like this:此外,拯救这样的一切:
def show
begin
@article = Article.find(params[:id])
rescue
render root
end
end
...is generally viewed as code smell. ...通常被视为代码气味。 There are a lot of articles on the interwebs about why.
网上有很多关于为什么的文章。
You can handle the exception from ArticlesController
but I advise put the code at ApplicationController
like this:您可以处理来自
ArticlesController
的异常,但我建议将代码放在ApplicationController
中,如下所示:
rescue_from ActiveRecord::RecordNotFound do
redirect_back fallback_location: root_path, alert: t("resource_not_found")
end
Maybe You can code like this:也许你可以这样编码:
def show
@article = Article.find_by(id: params[:id])
unless @article.present?
flash[:error] = "Not Found."
redirect_to root_path
end
end
why write so much code, while you can achieve with two lines only.为什么要写这么多代码,而你只需要两行就可以实现。
class ArticlesController < ApplicationController
def show
@article = Article.find_by(id: params[:id])
redirect_to root_path unless @article.present?
end
end
Your code should be written like this:你的代码应该这样写:
def show
begin
@article = Article.find(params[:id])
rescue
redirect_to root_path
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.