Rails确认

时间:2010-11-10 05:20:40

标签: ruby-on-rails ruby-on-rails-3

我有以下内容:

  validates :fname, :presence => true, :length => { :minimum => 2 }

如何添加消息?现在错误说“Fname太短(最少2个字符)”我想说出名字而不是Fname。

感谢?

3 个答案:

答案 0 :(得分:0)

你能试试吗?

validates :fname, :presence => true, :length => { :minimum => 2 },
:format => {
:message => 'your message.'}

答案 1 :(得分:0)

解决方案最终成为:

更改语言环境文件中字段的名称:

en: 
  activerecord: 
    attributes: 
      user: 
        fname: First name 

答案 2 :(得分:-1)

validates :fname, :presence => true, :length => { :minimum => 2 }, :message => "your message goes here"

<强>更新

如果您想使用友好的列名称,请使用this之类的代码:

class User < ActiveRecord::Base

  HUMANIZED_ATTRIBUTES = {
    :email => "E-mail address"
  }

  def self.human_attribute_name(attr)
    HUMANIZED_ATTRIBUTES[attr.to_sym] || super
  end

end

解决此问题的其他方法是采用此博客文章中描述的方法:http://www.softiesonrails.com/2008/4/23/better-messages-for-activerecord-validation-errors