Wednesday, February 18, 2009

i did not know you can do this

I've always found the default failure message for most validations really unhelpful. For example, with a "validates_inclusion_of :added_context, :in => VALID_ADDED_CONTEXTS" the failure message is:

ActiveRecord::RecordInvalid: Validation failed: Added context is not included in the list

However! You can use {{value}} and when specifying a message, for example:

  validates_inclusion_of :added_context, :in => VALID_ADDED_CONTEXTS,
      :message => "Value {{value}} not listed as a valid context in Interest::VALID_ADDED_CONTEXTS"

And now if I pass in an illegal value "your mom" we get something much more useful:

Added context Value your mom not listed as a valid context in Interest::VALID_ADDED_CONTEXTS

--
Love,
Fritz

No comments:

Post a Comment