【问题标题】:Rails 3 ActiveRecord Html entities in model attribute模型属性中的 Rails 3 ActiveRecord Html 实体
【发布时间】:2012-03-05 14:28:12
【问题描述】:

我已经通过这种方式重新定义了 ActiveRecord 模型的名称

  activerecord:
    attributes:
      user:
        email: "<b>Custom email</b>"
        password: "<b>Custom password</b>"

我想在我的浏览器中得到这样的输出:

“自定义邮箱不能为空

但得到

<b>Custom email</b> must be not empty

我该如何解决?

谢谢!

【问题讨论】:

    标签: ruby-on-rails activerecord customization


    【解决方案1】:

    通常在翻译中,您可以在键名后加上“_html”,以表示其输出不应被转义:

    user:
      email_html: "<b>Custom email</b>"
    

    您应该先尝试一下,但我认为 ActiveRecord 不会自动查找该键。

    相反,在您输出表单标签的地方,您可能需要自己提供翻译键:

    <%= f.label :email, t("activerecord.attributes.user.email_html") %>
    

    或者:

    <%= f.label :email, t("activerecord.attributes.user.email").html_safe %>
    

    我认为html_safe 仍然有效。

    【讨论】:

    • 安德鲁,感谢您的回复。但我不会以 的方式使用属性名称。属性名称由 ActiveRecord 自动使用。当我尝试给键 attr 加上后缀时,ActiveRecord 看不到这个键(
    • 还有其他涉及各种黑客的方法。尽管如果您所做的不超过您的示例所示(围绕&lt;b&gt; 中的标签文本),那么您应该通过 CSS 对其进行样式设置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多