# Validate Before RJS/Ajax Execution?

**URL:** <https://www.ruby-forum.com/t/validate-before-rjs-ajax-execution/83550>\
**Category:** Rails\
**Created:** [January 27, 2007, 2:35am UTC](https://www.ruby-forum.com/t/validate-before-rjs-ajax-execution/83550 "2007-01-27T02:35:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashiro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e5b9ba/32.png) [@ashiro](https://www.ruby-forum.com/u/ashiro)\
**Post date:** [January 27, 2007, 2:35am UTC](https://www.ruby-forum.com/t/validate-before-rjs-ajax-execution/83550/1 "2007-01-27T02:35:38Z")

</div>

Hi

I’ve got a model which validates\_presence\_of a field (its a comment  
form) and the new comment is added to the top of the comment listing  
with a blind\_down effect.

However, regardless of whether the field has been entered it still  
executes the RJS template. Is there some Ajax-level validation I can  
use thats catered for in Rails?

If someone could point me in the direction of a tutorial, method or  
anything of help I’d be very grateful.

Cheers.

Doug

---

<div class="post-metadata">

**Author:** ![ashiro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e5b9ba/32.png) [@ashiro](https://www.ruby-forum.com/u/ashiro)\
**Post date:** [January 27, 2007, 4:48pm UTC](https://www.ruby-forum.com/t/validate-before-rjs-ajax-execution/83550/2 "2007-01-27T16:48:56Z")

</div>

removed\_email\_address@domain.invalid wrote:

> I’ve got a model which validates\_presence\_of a field
> 
> However, regardless of whether the field has been  
> entered it still executes the RJS template.

Of course it does. Barring an error in the app, Rails will _always_  
generate a response to a browser request. In the case of non-RJS  
requests,  
Rails validations populate the @error\_messages hash which then gets  
rendered  
via a \<%= @error\_messages\_for @object %\> directive in your rhtml view.  
For  
RJS requests, the model still populates the @error\_messages hash.  
Problem  
is, when you’re using RJS you’re not going to render that view. The  
easiest  
way to handle your situation is to do the presence check in the  
controller  
and then ‘feed’ your RJS view, or call a different RJS view, depending  
on  
the result. If you want to send back an error message, use flash.now to  
avoid having the message hang around longer than one cycle. Might look  
something like…

def my\_ajax\_response  
if params[:thing\_to\_validate].empty?  
flash.now[:notice] =\> ‘that field can’t be empty’  
else  
object\_to\_save.field\_name = params[:thing\_to\_validate]  
object\_to\_save.save  
end  
end

hth,  
Bill

Is there some Ajax-level validation I can

---

<div class="post-metadata">

**Author:** ![ashiro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e5b9ba/32.png) [@ashiro](https://www.ruby-forum.com/u/ashiro)\
**Post date:** [January 27, 2007, 5:03pm UTC](https://www.ruby-forum.com/t/validate-before-rjs-ajax-execution/83550/3 "2007-01-27T17:03:58Z")

</div>

Oops. Depending on the specifics, you might need to use…

if params[:thing\_to\_validate].nil?  
…

Sorry about that.

Bill
