# Cancel Render after Ajax Response

**URL:** <https://www.ruby-forum.com/t/cancel-render-after-ajax-response/106288>\
**Category:** Rails\
**Created:** [June 8, 2007, 4:21am UTC](https://www.ruby-forum.com/t/cancel-render-after-ajax-response/106288 "2007-06-08T04:21:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![chamnap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/779978/32.png) [@chamnap](https://www.ruby-forum.com/u/chamnap)\
**Post date:** [June 8, 2007, 4:21am UTC](https://www.ruby-forum.com/t/cancel-render-after-ajax-response/106288/1 "2007-06-08T04:21:13Z")

</div>

Hello,

I have one problem related to Ajax and render :partial. I develop a  
small search application. When there are the results that match to the  
user’s search, it will render the partial views of the result search  
to replace part of the document. However, when the result is not  
matched, I want to display an alert box to the user back and keep the  
previous result’s search on the screen without erasing it. Is there a  
way to cancel the render? Normally, after Ajax response, it will erase  
the previous content. I want to know how to prevent this? Here is my  
code:

if @result.length \> 0  
render :partial =\> “result\_list”  
else

# …

end

Any idea? Please, help…

Thanks  
Chamnap

---

<div class="post-metadata">

**Author:** ![chamnap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/779978/32.png) [@chamnap](https://www.ruby-forum.com/u/chamnap)\
**Post date:** [June 8, 2007, 9:41am UTC](https://www.ruby-forum.com/t/cancel-render-after-ajax-response/106288/2 "2007-06-08T09:41:37Z")

</div>

On 6/8/07, Chamnap [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> the previous content. I want to know how to prevent this? Here is my  
> code:
> 
> if @result.length \> 0  
> render :partial =\> “result\_list”  
> else
> 
> # …
> 
> end
> 
> Any idea? Please, help…

I think that you made this test in your view, or you can define another  
partial

–  
Cyril M.

---

<div class="post-metadata">

**Author:** ![chamnap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/779978/32.png) [@chamnap](https://www.ruby-forum.com/u/chamnap)\
**Post date:** [June 8, 2007, 11:28am UTC](https://www.ruby-forum.com/t/cancel-render-after-ajax-response/106288/3 "2007-06-08T11:28:24Z")

</div>

render :nothing =\> true ? 😉

Chamnap wrote:

> Hello,

---

<div class="post-metadata">

**Author:** ![chamnap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/779978/32.png) [@chamnap](https://www.ruby-forum.com/u/chamnap)\
**Post date:** [June 8, 2007, 12:18pm UTC](https://www.ruby-forum.com/t/cancel-render-after-ajax-response/106288/4 "2007-06-08T12:18:55Z")

</div>

try something like this:

render :update do |page|  
if search\_results  
page.replace\_html ‘search\_results\_div’, :partial =\> ‘search\_results’  
else  
page.call ‘alert’, ‘No Results!’  
end  
end

---

<div class="post-metadata">

**Author:** ![chamnap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/779978/32.png) [@chamnap](https://www.ruby-forum.com/u/chamnap)\
**Post date:** [June 11, 2007, 4:51am UTC](https://www.ruby-forum.com/t/cancel-render-after-ajax-response/106288/5 "2007-06-11T04:51:43Z")

</div>

Hello,

Thanks javier ramírez and Titou T… It works now. Great, I learned  
something new from you both.

Thanks  
Chamnap

---

<div class="post-metadata">

**Author:** ![chamnap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/779978/32.png) [@chamnap](https://www.ruby-forum.com/u/chamnap)\
**Post date:** [June 8, 2007, 9:49am UTC](https://www.ruby-forum.com/t/cancel-render-after-ajax-response/106288/6 "2007-06-08T09:49:38Z")

</div>

Hi,

> to replace part of the document. However, when the result is not  
> matched, I want to display an alert box to the user back and keep the  
> previous result’s search on the screen without erasing it.  
> In your view code, in the “whatever\_remote” you are using just don’t use  
> an “:update” option. When you use update, you are telling rails to  
> replace the html of the given element with the response of the action  
> called. If the response is blank, then you get an empty element on  
> screen as a result.

If you are not using “update”, you are telling rails not to replace  
anything in your view, but to have instead the flexibility to use the  
javascript “document” object for whatever purpose you want (usually for  
multiple element updates). You can use rjs (or from the controller  
directly a render :update) in which you can use the “page” object as a  
wrapper for javascript “document”.

When you have a result, you can do a page.replace\_html your\_div\_id,  
your\_content  
when you lack a result you can do a page.alert (‘No results’)

That shoudl do the trick. Regards,

javier ramírez
