在关联错误消息中隐藏/删除模型名称



我一辈子都找不到如何删除"型号名称";在错误消息之前。通常情况下,这是可以的,但请听我说完。

我有一个叫"foo"的模型。我需要在某个时候重新命名它,但现在很麻烦。现在,我需要更改错误消息:;Foo不知道你多久领一次工资;。

# finance.rb
belongs_to :foo # this will be renamed in the future
[..]

我想我只需要编辑en.yml:

en:
activerecord:
errors:
models:
finance:
attributes:
foo:
required: "How often you get paid is missing"

这很有效,但我不需要在消息中显示模型的名称。好的,我可以做一些字符串替换,但那太难看了。是否可以只在en.yml中显示消息?

编辑:

错误显示为:

<% if @finance.errors.any? %>
<ul>
<% @finance.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>

这似乎是一个简单的解决方案。所以在我的情况下:

en:
errors:
format: "%{message}"
activerecord:
errors:
models:
finance:
attributes:
repayment_type:
required: "How often you get paid is missing"

这可能是一个重复的帖子,所以你可以标记为重复🙂

最新更新