Rails 没有属性的完整错误消息

Rails full error message without attribute

我无法删除错误消息中的属性。 (我想知道为什么这不是默认行为)

控制器

if @item.save
      ...
 else
   format.html { redirect_to [@profile, :items], alert: @item.errors.full_messages}
end

it.yml

 it:
  errors:
    format: "%{message}"

View.html

<% if alert %>
    <% Array.wrap(alert).each do |msg| %>
        <%=msg%>
    <% end %>
<% end %>

但我得到了这个输出

属性名称我的自定义消息

使用messages代替full_messages:

@item.errors.messages.values.flatten

作为 messages returns 哈希,其中键显示验证错误是什么,您可以简单地获取并展平所有值。

{ :email=>["Another record exists with this email."], ... } # for example