How to display validation error in Rails during an update operation?
I am having a form and I am trying to save something. Whenever I hit the save button, I am run开发者_如何学JAVAning an update query in the controller. Everything works fine.
I have put a validation in place for one of the text fields which takes text lengths of minimum 5. The error appears while I create the entry. However, when I try to just update, I am not getting any error in the page (validation works though - text is not saved to DB).
How to make sure validation error appears on the page. Following is the sample code.
def save_template
@template = get_template(params[:template])
@template.update_attributes(:label => params[:label])
#some actions later
end
Please help.
The update_attributes
method return true if update works and false instead.
So you just render edit template if update failed
def save_template
@template = get_template(params[:template])
unless @template.update_attributes(:label => params[:label])
render :edit
return
end
end
精彩评论