如何在 Rails 模型中设置人类可读的属性名称?
这似乎是一个非常简单的问题——当我使用 form_for 或 fields_for 帮助程序从我的模型生成标记时,如何修改我的模型以自定义为特定属性显示的字符串?
或多或少同样的问题之前被问过[1< /a>],但答案是“国际化”,这不是我想要做的,我只是想覆盖一两个人性化的属性名称。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许您可以在模型中沿着 (未测试)
该值将在标签中使用,除非您有相应的翻译,该翻译具有更高的优先级。
Maybe you'll be able to override human_attribute_name in your model along the lines of (not tested)
This value will be used in the labels unless you have corresponding translations, which have a higher priority.
您可以在 AR 类中覆盖
human_attribute_name 。请参阅此邮件列表帖子
You could override
human_attribute_name
in your AR class. See this mailing list post