我实际上很难弄清楚如何获得与特定关联相关的第一条错误消息。在我的表单中,我必须在每个相关字段下显示错误,例如:
= f.text_field :price, class: 'form-control'
- if @item.errors.has_key? :price
= content_tag :span, @item.errors[:price].first, class: 'form-error'
这适用于模型基础属性。但是,我的 Item 模型通过多态关联链接到另一个模型。显示时errors.full_messages
,我得到["Attachments name doit être rempli(e)"]
. 这是关系:
has_many :attachments, as: :attachable, dependent: :destroy
accepts_nested_attributes_for :attachments
validates :name, presence: true
我该怎么做才能获得此关联返回的错误?