Build custom error messages in model files (example: using the 'pluralize' method)

Posted by user502052 on Stack Overflow See other posts from Stack Overflow or by user502052
Published on 2011-07-01T07:31:01Z Indexed on 2011/07/01 8:22 UTC
Read the original article Hit count: 248

I am using Ruby on Rails 3.0.7 and I would like to use the pluralize method in a my model file in order to properly build custom error messages.

For example, I would like to do something like the following:

NAME_MIN_LENGHT = 2 # Is 2 but I plan to change that (maybe dynamically... if it is possible) in future development

validates :name,
  :length     => {
    :minimum        => NAME_MIN_LENGHT,
    :too_short      => "is too short (minimum is #{pluralize(NAME_MIN_LENGHT, "character")})",
  },

How can I do that? Is it advisable? Why?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby