这是我需要帮助的一个奇怪问题。从默认验证方法自定义错误消息rails
所以我想验证复选框在我的表格,以便ATLEAST一个必须选择:
...
<div class="form-group">
<%= f.collection_check_boxes(:topping_ids, Topping.all, :id, :name, include_hidden: false) do |b| %>
<ul>
<li><%= b.check_box %> - <%= b.label %></li>
</ul>
</div>
...
我的模型:
class Pizza < ApplicationRecord
has_many :pizza_toppings, dependent: :destroy
has_many :toppings, through: :pizza_toppings
validates_presence_of :name
validates_length_of :topping_ids, minimum: 1, message: "You must select at least 1 topping"
end
我的错误部分:
<% if object.errors.any? %>
<div id="error_explanation">
<div class="errors-alert text-center">
The form contains <%= pluralize(object.errors.count, "error") %>.
</div>
<ul>
<% object.errors.full_messages.each do |msg| %>
<li class="errors-alert-item text-center"><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
这是错误的样子:
如何更改或摆脱错误中的“Topping ID”。我学会了如何改变信息,但我不确定我会为“浇头ids”改变什么,它显然没有提供给用户。我不想从本地或其他类似的东西中更改完整的错误,因为我对其他表单使用了验证,因此我希望能够看到这些错误,这是我需要修改的唯一错误。
你必须提供在当地人的文件名替代为特定的属性。 –