在 Rails 中参与救援工作

发布于 2024-08-27 11:11:54 字数 400 浏览 12 评论 0原文

我正在创作以下作品;

def index
  @user = User.find(params[:id]) 
  rescue
    flash[:notice] = "ERROR"
    redirect_to(:action => 'index')
  else 
    flash[:notice] = "OK"
    redirect_to(:action => 'index')
end

现在,无论我是否有正确的 ID,我总是得到“OK”,我做错了什么?

当我在数据库中没有 ID 来显示“错误”时,我需要它。我也尝试使用 rescue ActiveRecord::RecordNotFound 但同样的情况发生。

感谢所有帮助。

I am working with the following piece;

def index
  @user = User.find(params[:id]) 
  rescue
    flash[:notice] = "ERROR"
    redirect_to(:action => 'index')
  else 
    flash[:notice] = "OK"
    redirect_to(:action => 'index')
end

Now I either case whether I have a correct ID or not, I am always getting "OK" in my view, what am I doing wrong?

I need that when I have no ID in the DB to show "ERROR". I have also tried to use rescue ActiveRecord::RecordNotFound but same happens.

All help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

骄兵必败 2024-09-03 11:11:54

仅当救援块中没有返回时,才会解释救援块结束后的所有代码。所以你可以在救援块结束时调用 return 。

def index
  begin
    @user = User.find(params[:id]) 
  rescue
    flash[:notice] = "ERROR"
    redirect_to(:action => 'index')
    return
  end
  flash[:notice] = "OK"
  redirect_to(:action => 'index')
end

def index
  @user = User.find(params[:id]) 
  # after is interpret only if no exception before
  flash[:notice] = "OK"
  redirect_to(:action => 'index')
rescue
  flash[:notice] = "ERROR"
  redirect_to(:action => 'index')
end

但就您而言,最好使用 rescue_from 或 < a href="http://api.rubyonrails.org/classes/ActionController/Rescue.html#M000415" rel="noreferrer">rescue_in_public

之类的

class UserController < ApplicationController
  def rescue_in_public(exception)
    flash[:notice] = "ERROR"
    redirect_to(:action => 'index')
  end

  def index
    @user = User.find(params[:id]) 
    flash[:notice] = "OK"
    redirect_to(:action => 'index')
  end
end

但是使用rescue_in_public并不是一个很好的建议

All code after the end of the rescue block is interpreted only if there are no returns in the rescue block. So you can call return at the end of your rescue block.

def index
  begin
    @user = User.find(params[:id]) 
  rescue
    flash[:notice] = "ERROR"
    redirect_to(:action => 'index')
    return
  end
  flash[:notice] = "OK"
  redirect_to(:action => 'index')
end

or

def index
  @user = User.find(params[:id]) 
  # after is interpret only if no exception before
  flash[:notice] = "OK"
  redirect_to(:action => 'index')
rescue
  flash[:notice] = "ERROR"
  redirect_to(:action => 'index')
end

But in your case the better is to use rescue_from or rescue_in_public

like

class UserController < ApplicationController
  def rescue_in_public(exception)
    flash[:notice] = "ERROR"
    redirect_to(:action => 'index')
  end

  def index
    @user = User.find(params[:id]) 
    flash[:notice] = "OK"
    redirect_to(:action => 'index')
  end
end

But the using of rescue_in_public is not really good advice

一枫情书 2024-09-03 11:11:54

只是 Rails Rescue 的总体答案:

我发现这非常酷:

@user = User.find(params[:id])  rescue ""

Just an overall Rails Rescue answer:

I found this to be very cool:

@user = User.find(params[:id])  rescue ""
筱果果 2024-09-03 11:11:54

如果没有具有该 iduser,则 User.find 将返回 nil。返回nil不是错误情况,也不会触发救援

If there is no user with that id, then User.find will return nil. Returning nil is not an error case and will not trigger a rescue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文