# ActiveRecord validation with ajax calls

**URL:** <https://www.ruby-forum.com/t/activerecord-validation-with-ajax-calls/136929>\
**Category:** Rails\
**Created:** [May 7, 2008, 2:04pm UTC](https://www.ruby-forum.com/t/activerecord-validation-with-ajax-calls/136929 "2008-05-07T14:04:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![skg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/76d3ee/32.png) [@skg](https://www.ruby-forum.com/u/skg)\
**Post date:** [May 7, 2008, 2:04pm UTC](https://www.ruby-forum.com/t/activerecord-validation-with-ajax-calls/136929/1 "2008-05-07T14:04:19Z")

</div>

Hi  
I have a model class ServiceDeskResolution.It has fields, code and  
solution.I have to make the solution not empty .So for that I wrote  
validates\_presence\_of :resolution  
in the model.So from the view when I click save nothing is saved to  
database.This is what I need.But no error messages are printed and the  
view goes to next window…How can I print error messages in the  
current window?

## View code (define\_sd\_resolution.rhtml) \<%= error\_messages\_for ‘service\_desk\_resolution’ %\> #This not working

-------code

\<%= submit\_to\_remote ‘save\_btn’, ‘Save’,  
:html =\> {:class =\> “itilbutton1”},  
:url =\> {:controller =\> ‘service\_desk’, :action =\>  
‘sd\_resolution\_save’, :id =\> @sd\_ticket.id, :sd\_resolution\_id =\>  
@sd\_resolution\_id}, :before =\>  
“checkResolution(document.getElementById(‘sd\_resolution\_service\_desk\_resolution\_code\_id’),  
‘Please Choose Resolution’);” %\>

controller code  
def sd\_resolution\_save

@service\_desk\_resolution=ServiceDeskResolution.new(params[:sd\_resolution])  
@service\_desk\_resolution.save  
render :action =\> “sd\_resolution\_save.rjs”

end

what I need is if !@service\_desk\_resolution.save how can i revert  
this back to the same define\_sd\_resolution.rhtml and print the  
validation message on that.  
Thanks in advance  
Sijo

---

<div class="post-metadata">

**Author:** ![skg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/76d3ee/32.png) [@skg](https://www.ruby-forum.com/u/skg)\
**Post date:** [May 7, 2008, 4:17pm UTC](https://www.ruby-forum.com/t/activerecord-validation-with-ajax-calls/136929/2 "2008-05-07T16:17:16Z")

</div>

I have good suggestion if you spend 5 minutes to  
[EXAMPLES – Live Validation](http://livevalidation.com/examples), and download their script :D. I hope  
it will help you.

I am not sure your code here, maybe it should be :

def sd\_resolution\_save

@service\_desk\_resolution=ServiceDeskResolution.new(params[:sd\_resolution])  
if @service\_desk\_resolution.save  
render :action =\> “sd\_resolution\_save.rjs”  
else  
render :action =\> ‘define\_sd\_resolution’  
#render :file =\> “#{RAILS\_ROOT}/public/define\_sd\_resolution.rhtml”  
# I am not sure with render file.  
end  
end

your code before dont have block to break error, so it will execute  
:action =\> “sd\_resolution\_save.rjs” although it is error or failed  
validation.

Good Luck,  
[http://Teapoci.Blogspot.com](http://Teapoci.Blogspot.com)  
Reinhart
