This is exactly how the error I want is raised in my model:
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
Even when I changed the error line to the following, it got the same result:
errors.add(:base, "You cannot add another client") errors.add_to_base("You cannot add another client")
Here's how error messages are analyzed (in the _error_messages part):
<% 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 %>
This is how the error message appears in my html:
There were errors: base: You cannot add another client
Edit 1: I want to get rid of the text "base:" in the actual message. Therefore, I would like to say There were errors: You cannot add another client .
source share