Cancel Render after Ajax Response

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

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

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

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

Hello,

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

Thanks
Chamnap