Ruby on rails Mongoid嵌入多个验证错误消息为多个对象+&引用;“无效”;

Ruby on rails Mongoid嵌入多个验证错误消息为多个对象+&引用;“无效”;,ruby-on-rails,ruby,mongoid,Ruby On Rails,Ruby,Mongoid,产生以下错误消息: class Report include Mongoid::Document embeds_many :figures end class Figure include Mongoid::Document embedded_in :report field :legend validates_presence_of :legend end 如何才能使错误消息中的复数表示一致?来自: 您可以随意命名您的关系,但如果类无法命名 Mongoid可以从名称

产生以下错误消息:

class Report
  include Mongoid::Document
  embeds_many :figures
end

class Figure
  include Mongoid::Document
  embedded_in :report
  field :legend
  validates_presence_of :legend
end
如何才能使错误消息中的复数表示一致?

来自:

您可以随意命名您的关系,但如果类无法命名 Mongoid可以从名称中推断出,反之亦然 您需要为宏提供一些附加选项以 告诉Mongoid如何把他们联系起来

你只需要使用

Figures is invalid
从:

您可以随意命名您的关系,但如果类无法命名 Mongoid可以从名称中推断出,反之亦然 您需要为宏提供一些附加选项以 告诉Mongoid如何把他们联系起来

你只需要使用

Figures is invalid

从错误到可读消息的转换由ActiveModel而不是Mongoid处理,并且嵌入式模型被认为只是属性本地化方面的属性

在这种情况下,如果在i18n文件中使用以下内容,则通过修改
human\u attribute\u name
为figures属性返回的内容,将得到单数形式而不是复数形式:

class Report
  include Mongoid::Document
  embeds_many :figures, class_name: "Figure"
end

class Figure
  include Mongoid::Document
  embedded_in :report, class_name: "Report"
  field :legend
  validates_presence_of :legend
end

从错误到可读消息的转换由ActiveModel而不是Mongoid处理,并且嵌入式模型被认为只是属性本地化方面的属性

在这种情况下,如果在i18n文件中使用以下内容,则通过修改
human\u attribute\u name
为figures属性返回的内容,将得到单数形式而不是复数形式:

class Report
  include Mongoid::Document
  embeds_many :figures, class_name: "Figure"
end

class Figure
  include Mongoid::Document
  embedded_in :report, class_name: "Report"
  field :legend
  validates_presence_of :legend
end

你的答案看起来不错,直到我试了一下,结果还是不起作用。Mongoid应该能够推断常见的“s”后缀复数。无论是否指定类名,该部分都可以正常工作。问题仍然存在于验证消息中。在我尝试之前,您的答案看起来不错,但它不起作用。Mongoid应该能够推断常见的“s”后缀复数。无论是否指定类名,该部分都可以正常工作。问题仍然存在于验证消息中。