rails:freeze:gems | freezzo.com

May 14, 2008

Full message for error_messages_for

Filed under: Ruby on Rails — Tags: , , , — Randy @ 8:05 pm

I found this somewhere while looking around for the easiest way to provide my own full message for the rails error_messages_for output.

Basically what we are going to do here is provide a humanized string for a variable of the model, and when the error message is printed out, it will display that message. This give more control instead of just having “Email is required.”

class Person < ActiveRecord::Base
 
  HUMANIZED_ATTRIBUTES = {
    :name => "Please provide a name for this person.",
    :email => "You must specify an email address."
  }
 
  def self.human_attribute_name(attr)
    HUMANIZED_ATTRIBUTES[attr.to_sym] || super
  end
 
  validates_presence_of :name,  :message=>''
  validates_presence_of :email, :message=>''
 
end

1 Comment »

  1. thank you!!!

    Comment by dashka — July 31, 2008 @ 8:19 am

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress