简体   繁体   中英

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

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM