How do I set human readable attribute names in a rails model?
-
16-10-2019 - |
Question
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.
Solution
You could override human_attribute_name
in your AR class. See this mailing list post
OTHER TIPS
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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow