包含验证的自定义错误消息

时间:2011-08-01 04:41:32

标签: ruby-on-rails-3 validation activerecord

我正在使用包含验证

validates :field_type, :inclusion => %w(SA LA RB CB SB Date)

现在当验证被解雇时,我得到了"Field type is not included in the list"。这对我没有任何意义。所以,我希望有自己的自定义消息说"This value is not included in Field Type."任何人都可以在这里指导我吗?

2 个答案:

答案 0 :(得分:17)

我想你想要:

validates :field_type, :inclusion => { :in => %w(SA LA RB CB SB Date),
                                       :message => "The value: %{value} is not included in Field Type." }

请参阅this post.

答案 1 :(得分:0)

尝试消息选项

validates :field_type, :inclusion => %w(SA LA RB CB SB Date), :message => "...."

http://guides.rubyonrails.org/v3.2.13/active_record_validations_callbacks.html#message