2

I would like to fix up some error messages my site generates. Here is the problem:

class Brand < ActiveRecord::Base
    validates_presence_of :foo
    ...
end

My goal is to make a message "Ticket description is required" instead of "Foo is required" or may not be blank, or whatever.

The reason this is so important is because lets say previously the field was ticket_summary. That was great and the server was coded to use that, but now due to crazy-insane business analysts it has been determined that ticket_summary is a poor name, and should be ticket_description. Now I don't necessarily want to have my db be driven by the user requirements for field names, especially since they can change frequently without functionality changes.

Is there a mechanism for providing this already?

To Clarify

:message => does not seem to be the correct solution, :message will give me "Foo [message]" as the error, I am looking to change the messages generated field name, not the actual message itself (though I will settle for having to change the whole thing).

3 Answers 3

9

So the answer was quite simple...

define

self.human_attribute_name(attribute) and return the human readable name:

def self.human_attribute_name(attribute)
    if attribute == :foo 
        return 'bar'
    end
end

I'd use a map of names of course. And thats that.

Sign up to request clarification or add additional context in comments.

3 Comments

This is a good solution. My solution is quite generic and it allows you to change the message and its format in a locale file rather than the ruby code.
Don't you need a call to super at the end if the attribute is not :foo?
Great, but this method has 2 arguments in rails 4, do it like: def self.human_attribute_name(attribute_key_name, options = {}) case attribute_key_name when :FOO return "Bar" else return super(attribute_key_name, options) end end
8

Add this to your config/locales/en.yml file:

en:
  activerecord:
    errors:

      # global message format 
      format: #{message}

      full_messages:
        # shared message format across models
        foo:
          blank: Ticket description is required

        # model specific message format
        brand:
          zoo:
            blank: Name is required

Now change your validation message to refer to the new message format:

validates_presence_of :bar, :message => "Empty bar is not a good idea"
validates_presence_of :foo, :message => "foo.blank"
validates_presence_of :zoo, :message => "brand.zoo.blank"

Lets try the code:

b = Brand.new
b.valid?
b.errors.full_messages
#=> ["Ticket description is required", 
#     "Empty bar is not a good idea",
#     "Name is required"]

As demonstrated above, you can customize the error message format at three levels.

1) Globally for all the ActiveRecord error messages

  activerecord:
    errors:
      format: #{message}

2) Shared error messages across models

  activerecord:
    errors:
      full_messages:
        foo:
          blank: Ticket description is required

3) Model specific messages

  activerecord:
    errors:
      full_messages:
        brand:
          zoo:
            blank: Name is required

3 Comments

That is not the issue. The message would be "+Foo+ this is my custom error message". I need "+Bar+ this is my custom error message" for the foo field.
I have updated my answer this should address your need. Take a look.
See my answer for a significantly simpler solution :P
0

You can do as given below:

# config/locales/en.yml
en:
  activerecord:
    attributes:
      brand:
        foo: "Ticket description"
    errors:
      models:
        brand:
          attributes:
            foo:
              blank: " is required"

Please check Fully custom validation error message with Rails for more details.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.