2011-08-06 189 views
15

我試圖做一個簡單的自定義驗證消息。驗證我使用的編譯和運行良好,但我沒有看到郵件中的任何改變:Rails:自定義驗證消息

validates :rating, :inclusion => { :in => 0..5 }, :presence => { :message => " must be within 0-5" }

的消息我得到的仍然是Rating is not included in the list

我需要驗證rating是目前和decimal之間0-5

回答

24

好吧,我解決了它。這是工作的驗證:

validates :rating, :inclusion => { :in => 0..5, :message => " should be between 0 to 5" } 
validates :rating, :presence => { :message => " cannot be blank" } 

,我在這裏加入此

validates :rating, :numericality => { :message => " should be a number" }

+0

權,參考:http://edgeguides.rubyonrails.org/active_record_validations_callbacks.html應該早點兒看了看,+ 1雖然 – apneadiving

+11

有什麼辦法擺脫字段名稱前綴? – IIllIIll

+0

爲了擺脫字段名稱前綴,你可以使用'message:nil',它不會有任何驗證的消息 – Miguelgraz