【发布时间】:2011-02-20 12:08:20
【问题描述】:
这就是我想要的错误在我的模型中的调用方式:
validate :number_of_clients
def number_of_clients
errors[:base] << "You cannot add another client" if
Authorization.current_user.plan.num_of_clients <= Authorization.current_user.clients.count
end
即使我将错误行更改为以下内容,它也会产生相同的结果:
errors.add(:base, "You cannot add another client")
errors.add_to_base("You cannot add another client")
这是错误消息的解析方式(在 _error_messages 部分中):
<% if object.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(object.errors.count, "error") %>
prohibited this user from being saved:</h2>
<p> There were problems with the following fields:</p>
<ul>
<% object.errors.full_messages.each_full do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
这是错误消息在我的 html 中的显示方式:
There were errors: base: You cannot
add another client
编辑 1:我想要删除实际消息中的文本“base:”。所以我想说There were errors: You cannot add another client。
【问题讨论】:
标签: ruby-on-rails-3 error-handling