Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Human Attribute Names in Rails 3.x? [duplicate]

Possible Duplicate:
Custom model attribute (column name) title in Ruby on Rails

I've been using this solution in Rails 2.x forever:

   HUMANIZED_ATTRIBUTES = {
    :email => "E-mail address"
  }

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

but this causes bizarre errors when getting the errors from an ActiveRecord instance. What is the correct way to get nice, human-readable names in Rails 3.x?

like image 619
Dan Rosenstark Avatar asked Jan 24 '11 20:01

Dan Rosenstark


2 Answers

I think the correct Rails 3 approach would be to use the translation api, but I'm using human_attribute_name as follows:

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

The call to super may be expecting the options argument to be there.

like image 121
Brian Deterling Avatar answered Nov 12 '22 02:11

Brian Deterling


Using human_attribute_name with the I18n framework is more simple I think :

In your view : User.human_attribute_name("email")

In your locale yml file :

en:
  activerecord:
    attributes:
      user:
        email: E-mail address
like image 44
tal Avatar answered Nov 12 '22 04:11

tal