How do I get rid of the word "base:" from my error message in Rails 3?

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 .

+4
source share
3 answers

Try

 errors.add_to_base("Your message") 
0
source
  validate :number_of_clients def number_of_clients errors.add(:base, "You cannot add another client") if Authorization.current_user.plan.num_of_clients <= Authorization.current_user.clients.count errors.blank? #You have to return false to not proceed end 

if: base appears, well, why don't you try:

 MyClass < ActiveRecord::Base attr_reader :my_mistery_field validate :number_of_clients def number_of_clients errors.add(:my_mistery_field, "You cannot add another client") if Authorization.current_user.plan.num_of_clients <= Authorization.current_user.clients.count errors.blank? #You have to return false to not proceed end end 

In your en.yml file (or lang.yml, regardless of your default lang application)

en.yml

 en: activerecord: attributes: my_class: my_mistery_field: "Don't touch on my mistery field" 

Of course, this is used for other things, but who says we can't spoof Rails?

Hope this helps you.

0
source

I solved this by adding the base: "" entry to the appropriate place under activerecord.attributes in my en.yml file.

0
source

Source: https://habr.com/ru/post/1340519/


All Articles