ruby-on-rails - Rails 验证错误消息未显示

我正在尝试为我的模型验证添加自定义错误消息。见下图:

validates :cost, numericality: { greater_than_or_equal_to: 0, message: :custom_error }

我的 en.yml 看起来像这样:

en:
  activerecord:
    errors:
      messages:
        custom_error: "some error translation"

从我读过的所有内容来看,这应该有效。但是,它仅在某些情况下有效。如果我执行以下操作,它似乎有效:

a = Item.new
a.valid? 
 # false
a.errors.messages
 # { :cost=>["some error translation"]}

在救援块中,我发现了该错误并打印了如下所示的消息:

def subtract_costs
  item.cost: -10
  item.valid?
  puts "error: #{item.errors.messages}"
  # Above outputs "error: {:cost=>["some error translation"]}"
rescue StandardError => error
  puts error.message
  # Above outputs "Validation failed:"
end

该消息总是传给 Validation failed: 。有谁知道什么可能导致错误消息为空白?我的项目中没有任何其他本地人或翻译。

回答1

编辑:我看到这里还有另一个问题。你的救援块有点混乱。在你的救援块中,你救援的是 Rails 运行时错误,而不是模型验证错误(除非你使用了 #save!#update! ,它们通过引发模型验证错误将其转变为运行时错误)。所以 error 在那种情况下是一个运行时错误,它不是为了携带模型验证错误的措辞而设计的。

所以你创造了一种“它不起作用”的情况,但在那种情况下它不应该起作用。这应该更清楚:

def subtract_costs
  item.cost = -10
  item.valid?
  puts "error: #{item.errors.messages}" # outputs "error: {:cost=>["some error translation"]}"
  puts item.errors.class.name
rescue StandardError => error
  puts error.message # outputs "Validation failed:"
  puts error.class.name
end

您没有正确查找 EN 语言环境消息。你会这样做:

validates :cost,
          numericality: { greater_than_or_equal_to: 0,
                          message: I18n.t('activerecord.errors.messages.custom_error') }

现在有一些方法可以完全避免在验证器上设置消息,但您需要将消息放在正确的位置。这是您的 EN 语言环境文件中的正确位置/层次结构。这是您的特定消息的正确位置:

en:
  errors:
    messages:
      greater_than_or_equal_to: 'some error translation'

一般来说,这里是放置自定义验证错误消息(以及我喜欢的措辞)的地方。

en:
  errors:
    format: "%{message}"
    messages:
      accepted:                 "The %{attribute} was not accepted; please accept the %{attribute}."
      blank:                    "The provided %{attribute} is blank; please enter a non-blank %{attribute}."
      confirmation:             "The provided %{attribute} does not match the corresponding entry; please re-check this entry against the original."
      empty:                    "The provided %{attribute} is empty; please enter a non-empty %{attribute}."
      equal_to:                 "The provided %{attribute} is incorrect; please enter exactly %{count}."
      even:                     "The provided %{attribute} is odd; please enter an even %{attribute}."
      exclusion:                "The provided %{attribute} is reserved; please enter a different %{attribute}."
      greater_than:             "The provided %{attribute} is too small; please provide a different %{attribute} greater than %{count}."
      greater_than_or_equal_to: "The provided %{attribute} is too small; please provide a different %{attribute} greater than or equal to %{count}."
      inclusion:                "The chosen %{attribute} is not available; please choose an available option." # Rails 4 and prior.
      inclusion_of:             "The chosen %{attribute} is not available; please choose an available option." # Rails 5 and later.
      invalid:                  "The provided %{attribute} is invalid; please enter a valid %{attribute}."
      in_between:               "The provided %{attribute} is outside of the accepted range; please enter a different %{attribute} within the range of %{min} to %{max}."
      less_than:                "The provided %{attribute} is too large; please provide a different %{attribute} less than %{count}."
      less_than_or_equal_to:    "The provided %{attribute} is too large; please provide a different %{attribute} less than or equal to %{count}."
      not_a_number:             "The provided %{attribute} is not numeric; please enter a numeric %{attribute}."
      odd:                      "The provided %{attribute} is even; please enter an odd %{attribute}."
      record_invalid:           "The %{attribute} is invalid. %{errors}"
      spoofed_media_type:       "The provided %{attribute} is invalid (often due to an incorrect file extension); please provide a valid %{attribute}, including an appropriate file extension."
      too_long:                 "The provided %{attribute} contains more than the %{count} available characters; please shorten the entry."
      too_short:                "The provided %{attribute} contains fewer than the %{count} required characters; please lengthen the entry."
      taken:                    "The provided %{attribute} is already taken; please enter a different %{attribute}."
      wrong_length:             "The provided %{attribute} contains the wrong amount of characters; please adjust the entry to exactly %{count} characters."

相似文章

随机推荐

最新文章