Rails 4自定义表单验证

时间:2013-12-16 16:03:00

标签: ruby-on-rails ruby ruby-on-rails-4

在我的Rails 4应用程序中,我宁愿没有默认的错误验证消息,所以我正在使用它(直到我最终得到客户端验证)。

validates :title, presence: true, :presence => { :message => "is required." }

默认情况下,它会呈现为

Title is required.

如何删除:标题,并提交我自己的消息,如:

:message => "Please add a title to your collection."

感谢任何帮助。一如既往,谢谢!

1 个答案:

答案 0 :(得分:2)

这是我的代码示例,您应该尝试errors.add:base,“您的消息在这里”

validate :or_fields



def or_fields
      if self.phone.blank? && self.phone2.blank? && self.email.blank? && self.email2.blank?
        errors.add :base, "You must enter either one Phone or one Email"
      end
    end