有什麼可以阻止模型被保存嗎?以確保屬性設置和模型保存
一種方法是使用異常升高的版本,它可以幫助解決這樣的問題:
def update
@model = Model.find(params[:id])
@model.update_attributes(params[:model])
redirect_to(model_path(@model))
end
這可以提高到一個更可靠的方法:
def update
@model = Model.find(params[:id])
# Use exception-throwing update_attributes!
@model.update_attributes!(params[:model])
redirect_to(model_path(@model))
rescue ActiveRecord::RecordNotFound
render(:partial => 'not_found')
rescue ActiveRecord::RecordInvalid
# Delegate back to edit action, something's invalid
edit
render(:action => 'edit')
end
有些情況下,update_attributes可能無法成功保存。
如果您可以使用相同的方法對相同的數據執行相同的更新,那麼這是特有的。
它可以幫助你,如果你可以提到:Rails版本,MySQL版本,發佈控制器代碼,保存模型時(before_save,任何觀察者等)在側面運行的任何其他代碼。 – Petros 2009-10-11 16:01:26
將代碼粘貼到更新記錄的地方...... – khelll 2009-10-11 17:10:12
可能是因爲你已經離開,attr_protected/attr_accessor'd你的模型。 – 2009-10-11 21:04:05