How do I set human readable attribute names in a rails model?

6k Views Asked by At

This seems like a really simple question -- when I'm using form_for or fields_for helpers to generate markup from my models, how can I modify my model to customize the string that appears for a particular attribute?

More or less the same question was asked before[1], but the answer was 'internationalize', and that's not what I'm trying to do, I just want to override one or two humanized attribute names.

2

There are 2 best solutions below

1
On BEST ANSWER

You could override human_attribute_name in your AR class. See this mailing list post

1
On

Maybe you'll be able to override human_attribute_name in your model along the lines of (not tested)

def self.human_attribute_name(*args)
  if args[0].to_s == "my_attribute"
    return "My Attribute"
  end
  super
end

This value will be used in the labels unless you have corresponding translations, which have a higher priority.