views:

21

answers:

1

Hi,

I am having a form and I am trying to save something. Whenever I hit the save button, I am running 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.

+1  A: 

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
shingara