DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> AJAX入門 >> AJAX詳解 >> Rails的RJS模板十九
Rails的RJS模板十九
編輯:AJAX詳解     

第三節 Model Validations – 模型驗證

當前狀態下,Expense Tracker將接受任何輸入並試圖創建Expense對象。問題是應用程序會讓無效的輸入阻塞。大多數情況下,這是我們代碼內的活動記錄沒有捕獲的異常,我們新添加的AJax指示器將只保持spinnig並且用戶不知道發生什麼。對於我們來說幸運的是Rails支持模型驗證。我們可驗證新Expense對象並返回一個友好的警告對話框來給用戶顯示任何問題。讓我們添加一些驗證給app/models/expense.rb內的Expense模型:

In its current state, the Expense Tracker will accept any input and try to create Expense objects. The problem is that the application chokes on invalid input. The most likely case is ActiveRecord tHRowing an exception that isn't caught by our code. Our newly-added AJax indicator will just keep spinning away and the user won't know what happened. Lucky for us, Rails has wonderful support model validations. We can validate the new Expense objects and return a nice alert box to the user showing any problems. Let's add some validations to the Expense model in app/models/expense.rb:

class Expense < ActiveRecord::Base

belongs_to :project

validates_presence_of :description

validates_numericality_of :amount

protected

def validate

errors.add(:amount, "must be greater than 0") unless amount.nil? || amount >= 0.01

end

end

這個驗證代碼將確保description不能為空,並且Expense的amount是個大於0的數字。現在我們只須修改RJS模板來顯示錯誤。打開app/vIEws/expenses/new.rJS並像下面修改模板:

This validation code will ensure that the description is not blank and that the amount of the Expense is a number greater than 0. Now we just have to modify our RJS template slightly to display the errors. Open up app/vIEws/expenses/new.rJS and modify the template to look like the following:

if @expense.new_record?

page.alert "The Expense could not be added for the following reasons:n" +

@expense.errors.full_messages.join("n")

else

page.insert_Html :bottom, 'expenses', :partial => 'expense'

page.visual_effect :highlight, "expense-#{@expense.id}"

page.form.reset 'expense-form'

end

代碼檢查Expense對象是否是個新記錄。如果它還是一個新記錄,那麼此處的保存必是有問題的,並且顯示錯誤。否則插入及突出顯示新Expense的動作被完成。注意表單只在操作成功時才會被重置。這樣的話用戶就不必在出現錯誤時,重新輸入description與amount。

The code checks to see if the Expense object is still a new record. If it is still a new object, then there must have been a problem saving it and the errors are shown. Otherwise, the normal action of inserting and highlighting the new Expense is performed. Notice that the form is only reset when the Operation was successful. This way the user doesn't have to retype the description and amount when there are errors.

在這個例子中我們只使用了一個簡單的JavaScript警告對話框來顯示錯誤。這是用RJS顯示錯誤的最簡單方式。另一個解決辦法是替換整個表單並插入error_messages_for()的渲染輸出到頁面上。這是個高級的內置Rails輔助方法,但也超出了RJS模板,所以你將必須在Expense對象被成功地添加之後,移出或隱藏渲染錯誤部分。

In this case we just used a simple JavaScript alert box to show the errors. This is the simplest method of displaying the errors with RJS. Another solution would be to replace the entire form and insert the rendered output of error_messages_for() into the page. This would take advantage the built-in Rails helpers, but also has more overhead in the RJS templates, as you would have to remove or hide the rendered error section after the Expense object was successfully added.

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved