ActiveRecord 'destroy' 方法在 Ruby on Rails 中返回一个布尔值? [英] ActiveRecord 'destroy' method returns a boolean value in Ruby on Rails?

查看:53
本文介绍了ActiveRecord 'destroy' 方法在 Ruby on Rails 中返回一个布尔值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Ruby on Rails 3,我想知道什么类型的返回将包含以下代码:

I am using Ruby on Rails 3 and I would like to know what type of return will have the following code:

@user.destroy

我需要用它来处理 successfault 的情况,如下所示:

I need that to handle cases on success and fault in someway like this:

if @user.destroy
  puts "True"
else
  puts "false"
end

有可能吗?如果是这样,如何?

推荐答案

在提问之前,您应该先尝试一下.你在那里得到的东西会很好用.

You should try what you're asking before asking. What you've got there will work just fine.

如果 destroy 有效,它将返回对象(它将在 if 语句中传递为 true),否则,它将返回 false 或引发异常,具体取决于失败的原因.

If the destroy works, it will return the object (which will pass as true in an if statement) and if not, it will return false or raise an exception, depending on why it failed.

这篇关于ActiveRecord 'destroy' 方法在 Ruby on Rails 中返回一个布尔值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆