firstruby

Master ruby with us

Archive for January, 2011

HUMANIZED_ATTRIBUTES in Rails 3

Posted by firstruby on January 21, 2011

In earlier version of Ruby on Rails 3, if we have some column like “dept_date”, in that case, error message is displayed as,

 

Dept date can’t be blank.

 

To change field/attribute name, we have to humanize this field. To humanized this field to display like “Departure Date” instead of “Dept date”  in earlier version of Rails 3 (i.e, in Rails 2 .x), then need to write like following,

HUMANIZED_ATTRIBUTES = {
:dept_date => “Departure Date”
}

def self.human_attribute_name(attr)
HUMANIZED_ATTRIBUTES[attr.to_sym] || super
end

In Rails 3, “def self.human_attribute_name(attr)” changes to “self.human_attribute_name(attr, options={})”, so above function will be as,

 

def self.human_attribute_name(attr, options={})
HUMANIZED_ATTRIBUTES[attr.to_sym] || super
end

 

Due to above code, dept_date field will be humanized to ‘Departure Date’.

Advertisements

Posted in Useful Links | 2 Comments »