0

我有一个联系表格,想根据失败的情况显示个别消息。我想使用 Flash 消息。因此,从我目前所读到的内容来看,我可以创建一个自定义方法(或者我认为它只是覆盖了现有的方法?)

例如,我想验证名称字段的存在

Class Message

attr_accessor :name
validates :name, :presence => true

def validate
 if self.name < 0
  errors.add(:name, "Name cannot be blank")
 end
end
end

在我的控制器中,我通常使用通用消息

flash.now.alert = "Please Ensure all Fields are filled in"

有没有办法调用验证失败的特定消息?

谢谢

4

2 回答 2

2

有一个可用的插件,你可以按照下面的网址

https://github.com/jeremydurham/custom-err-msg
于 2013-07-18T08:53:28.073 回答
1

检查方法验证,因为您可以传递带有所需消息的消息参数。

validates :name, :presence => {:message => 'The name can't be blank.'}
于 2013-07-18T08:59:08.750 回答