【问题标题】:Force rails (console and server) errors to display in full by default?强制rails(控制台和服务器)错误默认完整显示?
【发布时间】:2020-09-12 15:27:58
【问题描述】:

我经常需要查看与ROLLBACKs 相关的各种数据库相关的错误消息,类似于here

project = Project.find 118
project.assign_attributes(featured: true)
project.valid?
project.errors.full_messages

简单地附加.errors.full_messages 并不太费力,但是,当我们还没有实例化某个对象(在本例中为project)时,它变得更加乏味,因为必须实例化一个对象,这涉及更改代码(只需查看错误消息)。

例子:

User.create(name: "john", email: "john@gmail.com", .... etc)

必须重构为类似

user = User.new(name: "john", email: "john@gmail.com", .... etc)
user.save

# then type

user.errors.full_messages

所有这些都是为了简单地看到一条错误消息。

问题

是否有某种方法可以让错误消息总是显示(完整),而无需专门使用 .errors.full_messages 之类的东西来请求它们? (在 Rails 控制台和服务器中)

我会尝试任何事情 - 全局设置、宝石、黑客 - 不惜一切

【问题讨论】:

  • 希望看到错误“当我们没有实例化某个对象时”-> 你能详细说明一下吗?也许包括您想要的代码、有效和无效时的输出?
  • @JakeWorth tbh,我的代码是 User.create(name: "john", email: "john@gmail.com", .... etc),但它出错了,我更愿意更改为 user = User.new(name: "john", email: "john@gmail.com", .... etc) 以便简单地查看错误消息
  • @JakeWorth 它位于seeds.rb。我现在已经通过上述方式重构修复了特定错误,但我的问题是如何避免重构代码只是为了看到错误消息,也就是说,默认情况下总是显示错误消息(只要有它们无需通过.errors.. 询问即可进入控制台

标签: ruby-on-rails ruby-on-rails-6


【解决方案1】:

使用 bang (!)。

在需要很多东西(包括标题)的模型上,create 失败并触发您的回滚:

> Post.create(title: nil)
   (0.1ms)  BEGIN
  Post Exists (0.2ms)  SELECT  1 AS one FROM "posts" WHERE "posts"."slug" IS NULL LIMIT $1  [["LIMIT", 1]]
   (0.1ms)  ROLLBACK
=> #<Post:0x007fa44c934fc0
 id: nil,
 developer_id: nil,
 body: nil,
 created_at: nil,
 updated_at: nil,
 channel_id: nil,
 title: nil,
 slug: nil,
 likes: 1,
 tweeted: false,
 published_at: nil,
 max_likes: 1>

随着一声巨响,创建快速失败并引发RecordInvalid 错误:

> Post.create!(title: nil)
   (0.1ms)  BEGIN
  Post Exists (0.2ms)  SELECT  1 AS one FROM "posts" WHERE "posts"."slug" IS NULL LIMIT $1  [["LIMIT", 1]]
   (0.1ms)  ROLLBACK
ActiveRecord::RecordInvalid: Validation failed: Body can't be blank, Channel can't be blank, Developer can't be blank, Title can't be blank
from /Users/dev/.asdf/installs/ruby/2.3.3/lib/ruby/gems/2.3.0/gems/activerecord-5.0.1/lib/active_record/validations.rb:78:in `raise_validation_error'

要在 OP 上进行构建,可以使用 update_attributesupdate_attributes! 来产生两种行为:

> Post.first.update_attributes(title: nil)
  Post Load (0.2ms)  SELECT  "posts".* FROM "posts" ORDER BY "posts"."id" ASC LIMIT $1  [["LIMIT", 1]]
   (0.1ms)  BEGIN
  Developer Load (0.2ms)  SELECT  "developers".* FROM "developers" WHERE "developers"."id" = $1 LIMIT $2  [["id", 4], ["LIMIT", 1]]
  Post Exists (0.2ms)  SELECT  1 AS one FROM "posts" WHERE "posts"."slug" = $1 AND ("posts"."id" != $2) LIMIT $3  [["slug", "81e668bc4e"], ["id", 1], ["LIMIT", 1]]
   (0.1ms)  ROLLBACK
=> false

> Post.first.update_attributes!(title: nil)
  Post Load (0.2ms)  SELECT  "posts".* FROM "posts" ORDER BY "posts"."id" ASC LIMIT $1  [["LIMIT", 1]]
   (0.1ms)  BEGIN
  Developer Load (0.2ms)  SELECT  "developers".* FROM "developers" WHERE "developers"."id" = $1 LIMIT $2  [["id", 4], ["LIMIT", 1]]
  Post Exists (0.2ms)  SELECT  1 AS one FROM "posts" WHERE "posts"."slug" = $1 AND ("posts"."id" != $2) LIMIT $3  [["slug", "81e668bc4e"], ["id", 1], ["LIMIT", 1]]
   (0.1ms)  ROLLBACK
ActiveRecord::RecordInvalid: Validation failed: Title can't be blank
from /Users/dev/.asdf/installs/ruby/2.3.3/lib/ruby/gems/2.3.0/gems/activerecord-5.0.1/lib/active_record/validations.rb:78:in `raise_validation_error'

什么是!

! 在 Ruby 中通常意味着该方法将修改它所调用的对象。但是,ActiveRecord 有不同的约定; ! 方法“更严格,因为它们会引发异常。”

create docs validation docs

【讨论】:

  • 谢谢!什么时候简单地附加一个 ! 是安全/不安全的? (例如,我相信! 在其他情况下还有其他含义?)是否可以说我们可以使用数据库操作的 bang 来获得上述预期结果(打印错误消息)?
  • ActiveRecord 有不同的约定; bang 方法“更严格,因为它们会引发异常”guides.rubyonrails.org/active_record_basics.html#validations
  • @stevec 我想说在 ActiveRecord 上下文中,! 是安全的。我通常宁愿快速失败,但有例外。
  • 我想这取决于个人口味,但对我来说似乎没有任何理由想要错误消息。隐藏它似乎是一个奇怪的默认值。我想它会使控制台变得杂乱无章,但我认为这是一个小好处。我希望有一个全局设置always.show.errors = true。但我认为对所有 ActiveRecord 工作使用! 是一个很好的解决方案
  • @stevec:我明白了。这就是我们不同意的地方。此外,这更像是与您的“您必须将此代码重写为这种形式”的对比——不,您不需要。您可以继续使用create。另外,我敢保证你想要的行为可以用一个 5 LOC 补丁/mixin 来实现。但是,如果在您的情况下也可以接受因异常而失败,则无需涉及修补。
猜你喜欢
  • 1970-01-01
  • 2011-09-12
  • 2012-06-03
  • 1970-01-01
  • 1970-01-01
  • 2018-03-20
  • 2017-08-24
  • 2016-11-30
相关资源
最近更新 更多