我想修复我的网站生成的一些错误消息.问题来了:
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 验证...自定义字段名称
发布评论