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

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

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

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


当前回答

这里有另一种方法:

如果你使用这个模板:

<% 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不能为空"

其他回答

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

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

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

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

升级了@Federico回答为所有字段错误的通用答案。

在你的控制器中。

flash[:alert] = @model.errors.messages.values 
# [["field1_err1", "field1_err2"], ["field2_err1"], ["field3_err1"]]

与rails docs中一样,messages方法“返回属性的散列及其错误消息数组”。

然后,在表单中显示这些错误:

<% flash.each do |type, type_arr| %>
  <% type_arr.each do |msg| %>
    <ul>
      <li>
        <%= msg.to_sentence %>
      </li>
    </ul>
  <% 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"

这里有另一种方法:

如果你使用这个模板:

<% 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不能为空"