# Handling validation errors

**URL:** <https://www.ruby-forum.com/t/handling-validation-errors/176787>\
**Category:** Rails\
**Created:** [October 6, 2009, 8:32am UTC](https://www.ruby-forum.com/t/handling-validation-errors/176787 "2009-10-06T08:32:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sharankruthi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@sharankruthi](https://www.ruby-forum.com/u/sharankruthi)\
**Post date:** [October 6, 2009, 8:32am UTC](https://www.ruby-forum.com/t/handling-validation-errors/176787/1 "2009-10-06T08:32:41Z")

</div>

Hi,

I have edit page for a model, where edit button is given as  
submit\_to\_remote, where i specify in which div the content should be  
updated, but if validation fails, like if while editing, mandatory field  
is left empty, i need to update the error in some other div. how is it  
possible.

#controller  
def create  
@question = Question.new(params[:question])  
if @question.save  
@questions = Question.find(:all, :conditions =\> {:subject\_id =\>  
@question.subject\_id })  
#render :text =\> “saved”  
render :partial =\> “display\_all”  
#redirect\_to :action =\> ‘display\_all’, :subj\_id =\>  
@question.subject\_id  
else

```
    @question.errors.each_full do |msg|
      if @error != nil

        @error.concat(msg)
      else
        @error = msg;
      end
    end

  render :text => @error
end

```

end

This is called from view as  
\<%= submit\_to\_remote ‘create’, ‘Create’, :url =\> {:action =\> ‘create’},  
:update =\> ‘questions’ %\>

I need to update errors in another div say error, that will appear at  
the top of the page. how is it possible? can we do with render :update  
do |page| ? but it is showing me error

---

<div class="post-metadata">

**Author:** ![sharankruthi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@sharankruthi](https://www.ruby-forum.com/u/sharankruthi)\
**Post date:** [October 6, 2009, 8:52am UTC](https://www.ruby-forum.com/t/handling-validation-errors/176787/2 "2009-10-06T08:52:18Z")

</div>

Hi Sharanya,

Try the following which is working fine with the link\_to\_remote and  
remote\_form\_for

render :update do |page|  
if error occurs # write a condition which checks error occurs or  
not here  
page.show , “errors”  
page.replace\_html, “errors”, “Write a String after error  
occurs here.”  
else  
page.hide , “errors”  
# code u want to update if error not occurs.  
end  
end

Check development.log if u running application in development mode for  
AJAX errors.

---

<div class="post-metadata">

**Author:** ![sharankruthi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@sharankruthi](https://www.ruby-forum.com/u/sharankruthi)\
**Post date:** [October 6, 2009, 9:13am UTC](https://www.ruby-forum.com/t/handling-validation-errors/176787/3 "2009-10-06T09:13:35Z")

</div>

Salil G. wrote:

> Hi Sharanya,
> 
> render :update do |page|  
> if error occurs # write a condition which checks error occurs or  
> not here  
> page.show , “errors”  
> page.replace\_html, “errors”, “Write a String after error  
> occurs here.”  
> else  
> page.hide , “errors”  
> # code u want to update if error not occurs.  
> end  
> end
> 
> Check development.log if u running application in development mode for  
> AJAX errors.

Thankx a lot. It is working with submit\_to\_remote itself after taking  
:update attribute from views page.

---

<div class="post-metadata">

**Author:** ![sharankruthi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@sharankruthi](https://www.ruby-forum.com/u/sharankruthi)\
**Post date:** [October 6, 2009, 11:40am UTC](https://www.ruby-forum.com/t/handling-validation-errors/176787/4 "2009-10-06T11:40:50Z")

</div>

Hi Sharanya S.

```
 This you can also do with only a slight modification to 

```

submit\_to\_remote like

\<%= submit\_to\_remote ‘create’, ‘Create’, :url =\> {:action =\> ‘create’},  
:update =\> {:success =\> ‘success\_div’, :failure =\> ‘failure\_div’} %\>

No other change in controller So for example

if @design.save  
#do this  
else  
#do if fail  
end

Sijo
