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

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

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

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


当前回答

现在,设置人性化名称和自定义错误消息的公认方法是使用区域设置。

# config/locales/en.yml
en:
  activerecord:
    attributes:
      user:
        email: "E-mail address"
    errors:
      models:
        user:
          attributes:
            email:
              blank: "is required"

现在已经更改了“email”属性的人性化名称和存在验证消息。

可以为特定的模型+属性、模型、属性或全局设置验证消息。

其他回答

这里有另一种方法:

如果你使用这个模板:

<% if @thing.errors.any? %>
  <ul>
    <% @thing.errors.full_messages.each do |message| %>
      <li><%= message %></li>
    <% end %>
  </ul>
<% end %>

你可以像这样写你自己的自定义消息:

class Thing < ActiveRecord::Base

  validate :custom_validation_method_with_message

  def custom_validation_method_with_message
    if some_model_attribute.blank?
      errors.add(:_, "My custom message")
    end
  end

这样,由于下划线的存在,整个消息变成了“我的自定义消息”,但开头的额外空间是不明显的。如果你真的不想在开始的时候增加额外的空间,只需添加.lstrip方法。

<% if @thing.errors.any? %>
  <ul>
    <% @thing.errors.full_messages.each do |message| %>
      <li><%= message.lstrip %></li>
    <% end %>
  </ul>
<% end %>

的字符串。Lstrip方法将删除':_'所创建的额外空格,并将保持任何其他错误消息不变。

或者更好的是,使用自定义消息的第一个单词作为键:

  def custom_validation_method_with_message
    if some_model_attribute.blank?
      errors.add(:my, "custom message")
    end
  end

现在完整的消息将是“我的自定义消息”,没有额外的空间。

如果你想要完整的信息以一个大写的词开头,比如“URL不能为空”,这是不可能的。相反,试着添加一些其他的单词作为关键字:

  def custom_validation_method_with_message
    if some_model_attribute.blank?
      errors.add(:the, "URL can't be blank")
    end
  end

现在完整的信息是" URL不能为空"

关于被接受的答案和列表中的另一个答案:

我正在确认nanamkim的custom-err-msg的分支与Rails 5和区域设置一起工作。

您只需要用一个插入符号开始locale消息,它不应该在消息中显示属性名称。

模型定义为:

class Item < ApplicationRecord
  validates :name, presence: true
end

用下面的en.yml:

en:
  activerecord:
    errors:
      models:
        item:
          attributes:
            name:
              blank: "^You can't create an item without a name."

item.errors。Full_messages将显示:

You can't create an item without a name

你不能创建一个没有名称的项目

现在,设置人性化名称和自定义错误消息的公认方法是使用区域设置。

# config/locales/en.yml
en:
  activerecord:
    attributes:
      user:
        email: "E-mail address"
    errors:
      models:
        user:
          attributes:
            email:
              blank: "is required"

现在已经更改了“email”属性的人性化名称和存在验证消息。

可以为特定的模型+属性、模型、属性或全局设置验证消息。

试试这个。

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

上面的代码来自这里

在你看来

object.errors.each do |attr,msg|
  if msg.is_a? String
    if attr == :base
      content_tag :li, msg
    elsif msg[0] == "^"
      content_tag :li, msg[1..-1]
    else
      content_tag :li, "#{object.class.human_attribute_name(attr)} #{msg}"
    end
  end
end

当你想重写错误消息而不带属性名时,只需在消息前面加上^ like:

validates :last_name,
  uniqueness: {
    scope: [:first_name, :course_id, :user_id],
    case_sensitive: false,
    message: "^This student has already been registered."
  }