使用Rails我试图得到一个错误消息,如“歌曲字段不能为空”保存。做以下事情:

validates_presence_of :song_rep_xyz, :message => "can't be empty"

... 只显示“Song Rep XYW不能为空”,这是不好的,因为字段的标题不是用户友好的。如何更改字段本身的标题?我可以更改数据库中字段的实际名称,但我有多个“song”字段,我确实需要特定的字段名称。

我不想破坏rails的验证过程,我觉得应该有办法解决这个问题。


当前回答

一个我从未见过任何人提到过的独特方法!

我能够获得我想要的所有定制的唯一方法是使用after_validation回调来允许我操纵错误消息。

允许正常创建验证消息,您不需要尝试在验证助手中更改它。 创建一个after_validation回调,它将在后端到达视图之前替换该验证消息。 在after_validation方法中,你可以对验证消息做任何你想做的事情,就像一个普通的字符串!您甚至可以使用动态值并将它们插入到验证消息中。


#this could be any validation
validates_presence_of :song_rep_xyz, :message => "whatever you want - who cares - we will replace you later"

after_validation :replace_validation_message

def replace_validation_message
    custom_value = #any value you would like
    errors.messages[:name_of_the_attribute] = ["^This is the replacement message where 
    you can now add your own dynamic values!!! #{custom_value}"]
end

after_validation方法的作用域将比内置的rails验证帮助器大得多,因此您将能够访问正在验证的对象,就像您尝试使用object.file_name一样。这在您试图调用它的验证助手中不起作用。

注意:我们在验证的开始使用^来去掉属性名,就像@Rystraum引用这个gem时指出的那样

其他回答

如果你想把它们都列在一个漂亮的列表中,但又不想使用不友好的名字,你可以这样做……

object.errors.each do |attr,message|
  puts "<li>"+message+"</li>"
end

我建议安装David Easley最初编写的custom_error_message gem(或作为插件)

它可以让你做以下事情:

validates_presence_of :non_friendly_field_name, :message => "^Friendly field name is blank"

以下是我的代码,如果你仍然需要它,它可能对你有用: 我的模型:

validates :director, acceptance: {message: "^Please confirm that you are a director of the company."}, on: :create, if: :is_director?

然后我创建了一个helper来显示消息:

module ErrorHelper
  def error_messages!
    return "" unless error_messages?
    messages = resource.errors.full_messages.map { |msg|
       if msg.present? && !msg.index("^").nil?
         content_tag(:p, msg.slice((msg.index("^")+1)..-1))
       else
         content_tag(:p, msg)
       end
    }.join

    html = <<-HTML
      <div class="general-error alert show">
        #{messages}
      </div>
    HTML

    html.html_safe
  end

  def error_messages?
    !resource.errors.empty?
  end
end

Rails3 Code与完全本地化的消息:

在模型用户中。Rb定义验证

validates :email, :presence => true

在config locales /和. yml

en:  
  activerecord:
    models: 
      user: "Customer"
    attributes:
      user:
        email: "Email address"
    errors:
      models:
        user:
          attributes:
            email:
              blank: "cannot be empty"

试试这个。

class User < ActiveRecord::Base
  validate do |user|
    user.errors.add_to_base("Country can't be blank") if user.country_iso.blank?
  end
end

我在这里找到了这个。

Rails 3到6的更新:

validate do |user|
  user.errors.add(:base, "Country can't be blank") if user.country_iso.blank?
end

这是另一种方法。 您要做的就是在模型类上定义一个human_attribute_name方法。将列名作为字符串传递给该方法,并返回用于验证消息的字符串。

class User < ActiveRecord::Base

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

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

end

上面的代码来自这里