强制 rails(控制台和服务器)错误默认完整显示?
Force rails (console and server) errors to display in full by default?
我经常需要查看与各种数据库相关联的错误消息 ROLLBACK
s 以及所见的 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 控制台和服务器中)
我会尝试任何事情 - 全局设置、宝石、技巧 - 不惜一切代价
使用刘海 (!
)。
在需要很多东西(包括标题)的模型上,create
失败并触发回滚:
> Post.create(title: nil)
(0.1ms) BEGIN
Post Exists (0.2ms) SELECT 1 AS one FROM "posts" WHERE "posts"."slug" IS NULL LIMIT [["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 [["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_attributes
和 update_attributes!
来产生两种行为:
> Post.first.update_attributes(title: nil)
Post Load (0.2ms) SELECT "posts".* FROM "posts" ORDER BY "posts"."id" ASC LIMIT [["LIMIT", 1]]
(0.1ms) BEGIN
Developer Load (0.2ms) SELECT "developers".* FROM "developers" WHERE "developers"."id" = LIMIT [["id", 4], ["LIMIT", 1]]
Post Exists (0.2ms) SELECT 1 AS one FROM "posts" WHERE "posts"."slug" = AND ("posts"."id" != ) LIMIT [["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 [["LIMIT", 1]]
(0.1ms) BEGIN
Developer Load (0.2ms) SELECT "developers".* FROM "developers" WHERE "developers"."id" = LIMIT [["id", 4], ["LIMIT", 1]]
Post Exists (0.2ms) SELECT 1 AS one FROM "posts" WHERE "posts"."slug" = AND ("posts"."id" != ) LIMIT [["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 中的 !
通常意味着该方法将修改它所调用的 object。但是,ActiveRecord 有不同的约定; !
方法“更严格,因为它们会引发异常。”
我经常需要查看与各种数据库相关联的错误消息 ROLLBACK
s 以及所见的 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 控制台和服务器中)
我会尝试任何事情 - 全局设置、宝石、技巧 - 不惜一切代价
使用刘海 (!
)。
在需要很多东西(包括标题)的模型上,create
失败并触发回滚:
> Post.create(title: nil)
(0.1ms) BEGIN
Post Exists (0.2ms) SELECT 1 AS one FROM "posts" WHERE "posts"."slug" IS NULL LIMIT [["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 [["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_attributes
和 update_attributes!
来产生两种行为:
> Post.first.update_attributes(title: nil)
Post Load (0.2ms) SELECT "posts".* FROM "posts" ORDER BY "posts"."id" ASC LIMIT [["LIMIT", 1]]
(0.1ms) BEGIN
Developer Load (0.2ms) SELECT "developers".* FROM "developers" WHERE "developers"."id" = LIMIT [["id", 4], ["LIMIT", 1]]
Post Exists (0.2ms) SELECT 1 AS one FROM "posts" WHERE "posts"."slug" = AND ("posts"."id" != ) LIMIT [["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 [["LIMIT", 1]]
(0.1ms) BEGIN
Developer Load (0.2ms) SELECT "developers".* FROM "developers" WHERE "developers"."id" = LIMIT [["id", 4], ["LIMIT", 1]]
Post Exists (0.2ms) SELECT 1 AS one FROM "posts" WHERE "posts"."slug" = AND ("posts"."id" != ) LIMIT [["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 中的 !
通常意味着该方法将修改它所调用的 object。但是,ActiveRecord 有不同的约定; !
方法“更严格,因为它们会引发异常。”