2013-07-18 103 views
0

我有一個聯繫表單,並希望根據什麼失敗顯示個人消息。我想使用Flash消息。所以從我讀到目前爲止,我可以創建自定義方法(或我認爲它只是覆蓋了一個到位?)自定義驗證Rails模型

因此,舉例來說,我想驗證名稱字段的存在

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" 

有沒有一種方法來調用驗證失敗的特定消息?

感謝

回答

2

有可用的插件,U可以按照以下網址

https://github.com/jeremydurham/custom-err-msg 
1

檢查方法validates因爲你可以通過與所需消息的消息參數。

validates :name, :presence => {:message => 'The name can't be blank.'} 
+0

我該如何在閃光警報中顯示消息? – Richlewis

+0

@Richlewis查看這個[application](https://github.com/RailsApps/rails3-bootstrap-devise-cancan)作爲例子。因此,如果您有任何疑問,您可以獲得完整的申請。 –