ActiveRecord 验证...自定义字段名称

编程入门 行业动态 更新时间:2024-10-23 11:23:00
本文介绍了ActiveRecord 验证...自定义字段名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想修复我的网站生成的一些错误消息.问题来了:

I would like to fix up some error messages my site generates. Here is the problem:

class Brand < ActiveRecord::Base validates_presence_of :foo ... end

我的目标是制作一条消息需要票证描述"而不是需要 Foo"或者不能为空或其他任何内容.

My goal is to make a message "Ticket description is required" instead of "Foo is required" or may not be blank, or whatever.

这如此重要的原因是因为我们之前说该字段是ticket_summary.这很好,服务器被编码为使用它,但现在由于疯狂的业务分析师已经确定ticket_summary 是一个糟糕的名字,应该是ticket_description.现在我不一定想让我的数据库由用户对字段名称的要求驱动,特别是因为它们可以在不更改功能的情况下频繁更改.

The reason this is so important is because lets say previously the field was ticket_summary. That was great and the server was coded to use that, but now due to crazy-insane business analysts it has been determined that ticket_summary is a poor name, and should be ticket_description. Now I don't necessarily want to have my db be driven by the user requirements for field names, especially since they can change frequently without functionality changes.

是否已有提供此功能的机制?

Is there a mechanism for providing this already?

澄清

:message => 似乎不是正确的解决方案,:message 会给我Foo [message]"作为错误,我希望更改消息生成的字段名称,而不是实际的消息本身(尽管我将满足于不得不改变整个事情).

:message => does not seem to be the correct solution, :message will give me "Foo [message]" as the error, I am looking to change the messages generated field name, not the actual message itself (though I will settle for having to change the whole thing).

推荐答案

所以答案很简单...

定义

self.human_attribute_name(attribute) 并返回人类可读的名称:

self.human_attribute_name(attribute) and return the human readable name:

def self.human_attribute_name(attribute) if attribute == :foo return 'bar' end end

我当然会使用姓名地图.就是这样.

I'd use a map of names of course. And thats that.

更多推荐

ActiveRecord 验证...自定义字段名称

本文发布于:2023-10-23 22:50:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1522235.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   字段   名称   ActiveRecord

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!