简体   繁体   English

在Rails 4中,有没有一种方法可以覆盖to_s方法以正确显示url字段?

[英]In Rails 4, is there a way to override the to_s method to display a url field correctly?

My Profile model has a portfolio_url field. 我的Profile模型有一个portfolio_url字段。

In error messages (as well as other places), it's displayed as 'Portfolio Url'. 在错误消息中(以及其他位置),它显示为“ Portfolio Url”。

Is there a way to customize this in one place (my guess is overriding a to_s method) so it's displayed as Portfolio URL ? 有没有一种方法可以在一个地方进行自定义(我的猜测是覆盖to_s方法),以便将其显示为Portfolio URL

There is no need to override a method to change the label of an ActiveRecord attribute. 无需重写更改ActiveRecord属性标签的方法。 Just add the name you want to use to your translation file: 只需将要使用的名称添加到翻译文件中即可:

# in config/locales/en.yml
en:
  activerecord:
    attributes:
      profile:
        portfolio_url: "Portfolio URL"

Read more about ActiveRecord translations in the Rails Guide . 在Rails指南中阅读有关ActiveRecord转换的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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