2012-12-29 36 views
1

我尋覓了很多關於這個問題沒有運氣用戶...Rails的:如何在before_update方法引發錯誤

我有一個before_update方法的模型,我不能代替它與驗證程序,因爲實際的方法檢查許多事情的存在和更新許多人。我的問題是,如果來自before_update方法的進程失敗,我該如何提出錯誤?喜歡的東西:

def update_status 
    if !(many verifications and updates) 
     self.errors[:base] << "Could not update if ...." 
    end 
end 

隨着的Abobe代碼我從頁面加載後控制器的更新通知,但我想說明從before_update方法的錯誤。我如何向用戶顯示錯誤?

非常感謝!

回答

0
def update_status 
    if !(many verifications and updates) 
     raise "Could not update if ...." 
    end 
end 

現在確實顯示控制器上的錯誤信息,你可以做這樣的事情:

begin 
    @myObject.update_status 
rescue => ex 
    flash[:notice] = ex.message 
end 
+0

非常感謝喬!我以前嘗試過,它給我的錯誤,但不是我可以在用戶界面中顯示的通知或錯誤。還有另一種方法可以做到嗎? – JPG

+0

你想向網站的用戶顯示異常消息嗎? – Joe

+0

是的,讓我們說,作爲一個通知或類似的... – JPG

相關問題